python - “TypeError:'int' object is not subscriptable”尝试对字符串进行切片时
问题描述
我想浏览表格上的列表,该列表采用另一个列表的坐标。但它返回一个错误:“'int' object is not subscriptable”即使我更改了列表。
Text = [
["B","o","n","j"],
["o","u","r"," "],
["ç","a","v","a"],
["b","i","e","n"],
]
Cle = [
['2','5','6','7'],
['9','3','1','5'],
['7','1','7','8'],
['8','4','3','0'],
]
for a in range(0,4):
for b in range(0,4):
Text[a][b] = ( int( ord(Text[a][b])) ^int(Cle[a][b]))
def Affichage(TextAffichage):
print('[[', '],\n['.join([','.join(["'{:4}'".format(item) for item in row]) for row in TextAffichage]))
print("],\n]")
Affichage(Text)
print("*"*30)
# Substitution of the text with a data box de in decimal base
SBOX = [
['77', '23', '4', '34', '26', '34', '81', '70', '26', '86'],
['64', '96', '66', '28', '4', '2', '21', '90', '79', '83'],
['25', '13', '60', '63', '82', '36', '86', '69', '43', '17'],
['13', '80', '21', '62', '80', '92', '62', '9', '69', '40'],
['21', '76', '90', '10', '28', '32', '21', '33', '18', '56'],
['73', '50', '56', '53', '1', '7', '96', '52', '59', '98'],
['13', '80', '98', '90', '19', '30', '35', '52', '10', '55'],
['23', '85', '57', '42', '64', '50', '30', '92', '74', '86'],
['79', '4', '60', '50', '26', '76', '78', '55', '14', '32'],
['76', '43', '89', '12', '36', '49', '23', '28', '64', '37']]
x = []
y = []
Texte = []
for i in range(0,4):
for m in range(0,4):
x = (Text[i][m][0])
y = (Text[i][m][1])
Texte(i)[m] = SBOX[int(x)][int(y)]
Affichage(Text)
它返回什么:
[[ ' 64',' 106',' 104',' 109'],
[' 102',' 118',' 115',' 37'],
[' 224',' 96',' 113',' 105'],
[' 106',' 109',' 102',' 110'
],
]
******************************
Traceback (most recent call last):
File "C:Downloads\petit test sur texte.py", line 59, in <module>
x = (Text[i][m][0])
TypeError: 'int' object is not subscriptable
解决方案
问题Text[i][m][0]
在于它Text[i][m]
是一个整数,如下所示,并且错误表明您不能为整数下标。在这里你可以看到它Text
只是二维的,这就是为什么Text[i][m][0]
不起作用;它试图在第三个维度上建立索引。
In [1]: Text = [
...: ["B","o","n","j"],
...: ["o","u","r"," "],
...: ["ç","a","v","a"],
...: ["b","i","e","n"],
...: ]
...:
...: Cle = [
...: ['2','5','6','7'],
...: ['9','3','1','5'],
...: ['7','1','7','8'],
...: ['8','4','3','0'],
...: ]
...:
...: for a in range(0,4):
...: for b in range(0,4):
...: Text[a][b] = ( int( ord(Text[a][b])) ^int(Cle[a][b]))
...:
In [2]: Text
Out[2]:
[[64, 106, 104, 109],
[102, 118, 115, 37],
[224, 96, 113, 105],
[106, 109, 102, 110]]
不过,我不确定您为什么期望 Text 是三维的。
推荐阅读
- html - 如何在WordPress的帖子中添加下拉菜单?
- java - web3j - EventEncoder.encode 很慢
- flutter - 如何防止 FloatingActionButton 占用任何空间
- r - 通过使用 MRE 降序 n 来订购 ggplot 条形图(其他地方尚未回答)
- amazon-web-services - 文件如何以 ansible 角色在 Files 文件夹中运行?
- python - 使用欧几里得距离在数组中查找最相似的索引
- google-apps-script - 作为初学者,我不明白为什么这个脚本不起作用。任何了解我的谷歌脚本问题的人?
- java - 使用 Java 在 Android App 中部署对象检测 ssd v2 模型
- python - 在python中制作一个副本
- javascript - 使用 CloudFront 功能时如何进行 base64 编码/解码?