简体   繁体   English

当我尝试在python的for循环内装入数组时发生错误

[英]Error occurs when I try to mount an array inside the for loop in python

Error occurs when I try to mount an array inside the for loop in python 当我尝试在python的for循环内装入数组时发生错误

I am in a transition of language, so small doubts arise in the day to day. 我正在转换语言,因此每天都会产生一些小的疑问。

TypeError: list indices must be integers or slices, not str TypeError:列表索引必须是整数或切片,而不是str

data = []
for index, linha in enumerate(reg2):
    data['lista'][index]['name'] = linha.name,
    data['lista'][index]['document'] = linha.document

    data.append(data)

You need a dictionary of lists of dictionaries, not a list. 您需要字典列表的字典,而不是列表。 Do not enumerate anything; 不要枚举任何东西; in fact, you do not even need a loop: 实际上,您甚至不需要循环:

docs = [{'name': linha.name, 'document': linha.document} for linha in reg2]
data = {'lista' : docs}

暂无
暂无

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

相关问题 当我尝试安装到 python3 中的线程模块时,会发生错误: - When I try to install to thread module in python3 that error occurs: “NoneType”对象没有“插入”属性。 当我尝试在数组中插入弹出值时发生此错误 - 'NoneType' object has no attribute 'insert'. This error occurs when I try to insert popped value in array 当您尝试将数据类型分配给python中的输入时发生错误 - An error occurs when you try to assign a data type to a input in python 错误发生时运行功能,无需尝试 - Run function when error occurs without try 当我尝试使用函数参数作为列表索引时,出现错误“列表索引必须是整数或切片,而不是元组” - Error “list indices must be integers or slices, not tuple” occurs when I try to use function parametrs as list indices AttributeError: 'NoneType' 对象没有属性 'name' 错误在我尝试获取 docx 文件的标题时发生 - AttributeError: 'NoneType' object has no attribute 'name' error occurs when I try to get a heading of a docx file Python:发生错误和消失时打印 - Python: Print when error occurs and when it disappears 当我尝试对这个numpy数组求和时,为什么Python会崩溃? - Why does Python crash when I try to sum this numpy array? 当我尝试识别名词和动词时,Python nltk 显示错误 - Python nltk shows error when i try to identify nouns and verbs 当我尝试在 python 中制作 3D 图形时输入错误 - Type Error when I try to make a 3D graph in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM