简体   繁体   English

列表 object 上的“TypeError: 'tuple' object 不支持项目分配”

[英]“TypeError: 'tuple' object does not support item assignment” on a list object

I imported a table from a database by using sqlite3, and then converted it to a list, even the type() function returned that the class was a list, but it still gave me that error message when I tried to change one of the cells我使用 sqlite3 从数据库中导入了一个表,然后将其转换为一个列表,即使 type() function 返回 class 是一个列表,但是当我尝试更改其中一个单元格时它仍然给了我该错误消息

Here's my code:这是我的代码:

import sqlite3

conn = sqlite3.connect('test.db')
c = conn.cursor()
c.execute("SELECT * FROM table1")
table = list(c.fetchall())
print(type(table))

table[0][0] = '10'

Output: Output:

<class 'list'>
Traceback (most recent call last):
  File "PATH", line 9, in <module>
    table[0][0] = '10'
TypeError: 'tuple' object does not support item assignment

Can anyone please tell me what's wrong?谁能告诉我怎么了?

You can convert your list of tuples to a list of lists using the map function.您可以使用map function 将元组列表转换为列表列表。

table = list(map(list, table))

Then you will be able to reassign table[0][0] , assuming such an element exists.然后,假设存在这样的元素,您将能够重新分配table[0][0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM