Search code examples
pythonlistnumpyreplace

'list' object has no attribute 'replace'


I'm working on some code to encrypt a word into numbers from the polybius square, however I need the output to be the two numbers right next to each other e.g. 41 instead of a coordinate format (4, 1). Item.replace hasn't been working to get rid of the unnecessary characters, as it returns 'list' object has no attribute 'replace'. How can i get the output that i want?

import numpy as np
polybius_square = np.array([["a","b","c","d","e"],
                  ["f","g","h","i","j"],
                  ["k","l","m","n","o"],
                  ["p","q","r","s","t"],
                  ["u","v","w","x","y"]])
word = str(input("Enter a word"))
wordletters = list(word)
numbers = []
print(wordletters)
print(len(wordletters))
for count in range(0,len(wordletters)):
  solutions = np.argwhere(polybius_square == wordletters[count])
  solutions = solutions.tolist()
*  solutions = [item.replace(3, 10) for item in solutions]*
  print(solutions)
  numbers.append(solutions)
  print(numbers)
print(*numbers, sep=" ")

i tried a couple ways, but none let me get rid of any characters from the items in the lists.


Solution

  • The encryption algorithm isn't really the question. You have an np.array in this format:

    import numpy as np
    
    arr = np.array([[4, 3]])
    print(arr)
    

    Output:

    [[4 3]]
    

    and want the two-digit value:

    num = arr[0][0] * 10 + arr[0][1]
    print(num)
    

    Output:

    43