繁体   English   中英

python TypeError:列表索引必须是整数,而不是列表

[英]python TypeError: list indices must be integers, not list

TypeError: list indices must be integers, not list

这是有问题的孩子;

['19 16 7 197 161 127 38 28 18 180 119 90 202 124 102 215 151 116 255 235 208 252 216 192 244 208 174 84 36 26 193 158 126 170 118 81'] <type 'list'>

作为字符串打印到文件中。 如您所见,它是一个列表,由数字组成,但未标识为整数。 当数据设置为numpy模块和corrcoef()函数时,出现错误消息。

如果您能帮助我理解这一点,那就太好了,谢谢。 加里

我尝试过的事情是:

a = [a**i for i in a1]
a = list(map(int,a1))
a = [a1[i] for i in a1]
a = [int(i) for i in a1]

我不太了解如何处理给定的列表,但是如果要将其转换为整数列表,可以执行以下操作;

a = ['19 16 7 197 161 127 38 28 18 180 119 90 202 124 102 215 151 116 255 235 208 252 216 192 244 208 174 84 36 26 193 158 126 170 118 81']
a = map(int, a[0].split())

所以试试这个:

import re

a = re.findall(r'\d+', a[0]) # regex better for parsing more complex

或尝试以下方法:

a = [int(i) for i in a.split()] # a bit simpler in this scenario

暂无
暂无

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

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