TypeError: unsupported operand type(s) for %: 'list' and 'int' with np.mod

I have this error:

File "C:\Users\dimak\PycharmProjects\HelloWorld\kl.py", line 13, in encryptAffine linearNumericEncryptedWordArray = np.mod(keyMatrix.dot(X), 33) TypeError: unsupported operand type(s) for %: 'list' and 'int' and i really dont know how to fix this.

import numpy as np
def encryptAffine(keyMatrix, keyVector, inputWord, alphabet, alphabetReversed):
    encodeWordToNumberic = []
    for char in inputWord:
        encodeWordToNumberic.append(alphabet[char])

        numericWordArray = []
        numericWordArray.append(encodeWordToNumberic[::2])
        numericWordArray.append(encodeWordToNumberic[1::2])
        X = np.array(numericWordArray, dtype="object")

        linearNumericEncryptedWordArray = np.mod(keyMatrix.dot(X), 33)

        affineNumericEncryptedWordArray = np.mod(np.add(linearNumericEncryptedWordArray, keyVector), 33)
        print(affineNumericEncryptedWordArray.T)
        encryptedWord = ''
        for i in affineNumericEncryptedWordArray.T:
            for j in i:
                encryptedWord += alphabetReversed[j]

    return encryptedWord

And main function with alphabet:

def main():
    alphabet = {'а': 0, 'б': 1, 'в': 2, 'г': 3, 'ґ': 4, 'д': 5, 'е': 6, 'є': 7, 'ж': 8, 'з': 9, 'и': 10, 'і': 11,
                'ї': 12, 'й': 13, 'к': 14, 'л': 15, 'м': 16, 'н': 17, 'о': 18, 'п': 19, 'р': 20, 'с': 21, 'т': 22,
                'у': 23, 'ф': 24, 'х': 25, 'ц': 26, 'ч': 27, 'ш': 28, 'щ': 29, 'ь': 30, 'ю': 31, 'я': 32}

    alphabetReversed = dict((v, k) for k, v in alphabet.items())

    A = np.array([[16, 16], [27, 31]])
    S = np.array([[2], [4]])
    word = 'перетворення'

    outputWord = encryptAffine(A, S, word, alphabet, alphabetReversed)

    print("Вхідне слово: {}\nЗашифроване слово: {}".format(word, outputWord))
if __name__ == "__main__":
    main()

Upvotes: 0

Views: 488

Answers (1)

Ananda
Ananda

Reputation: 3270

You are trying to use np.mod with the first element as a list. This isn't supported. Instead, you need to pass each element of the list into the mod function and make a list afterwards.

You can do this.

linearNumericEncryptedWordArray = [np.mod(x, 33) for x in keyMatrix.dot(X)]

Note that you will likely need to do this in the subsequent line as well.

Upvotes: 1

Related Questions