我正在编写一些代码来将单词加密为波利比乌斯方中的数字,但是我需要输出是彼此相邻的两个数字,例如41 而不是坐标格式 (4, 1)。 Item.replace 尚未消除不必要的字符,因为它返回“list”对象没有属性“replace”。我怎样才能得到我想要的输出?
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=" ")
我尝试了几种方法,但没有一种方法可以让我从列表中的项目中删除任何字符。
当您打印列表时,这只是列表的字符串表示。 对包含数字的列表本身进行操作:
solutions = np.argwhere(polybius_square == wordletters[count])
number = solutions[0][0] * 10 + solutions[0][1] # It's a 2D ndarray, e.g. [[4 3]]
numbers.append(number)