提问人:andrewarnier 提问时间:10/24/2023 最后编辑:andrewarnier 更新时间:10/25/2023 访问量:44
“tuple”对象不支持对数组进行项目赋值
'tuple' object does not support item assignment on array
问:
我想从我的数据库中选择,当我尝试更改其中一个单元格时
这是我的代码:
command = "select desc ,city,datetime,loc from mytable'"
cursor.execute(command)
result = cursor.fetchall()
i = 0
for x in result:
myary.append(result[i])
i= i+1
my_list = list(myary)
for y in range(0,len(myary)):
sip = myary[y][0].split("/")
my_list [y][0]=sip[0]
myary = tuple(my_list)
输出:
'tuple' object does not support item assignment
谁能告诉我怎么了?
答:
0赞
mandy8055
10/25/2023
#1
元组
在 Python 中是不可变的。在代码中,您正在尝试修改一个。要修复它,您可以将其转换为列表
。像这样:
command = "select desc ,city,datetime,loc from mytable'"
cursor.execute(command)
result = cursor.fetchall()
my_list = [list(x) for x in result] # Convert each tuple to a list
for y in range(0,len(my_list)):
sip = my_list[y][0].split("/")
my_list[y][0]=sip[0]
myary = tuple(my_list) # Convert them back to a tuple of tuples
评论
a = (0, 1, 2)
a[0] = 3
my_list[y]