[英]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.