简体   繁体   English

TypeError:列表索引必须是整数或切片,而不是元组

[英]TypeError: list indices must be integers or slices, not tuple

fvecs = [] fvecs = []

for line in open(filename):
    stats = line.split(',')
    labels.append(int(stats[0]))
    fvecs.append([float(x) for x in stats[5,6,12,27,29,37,39,41]])

I have a big csv. 我的csv大。 file that I am using as a dataset containing 43 columns and hundreds of rows, I am attempting to extract specific columns to be used as individual records and I can't seem to work this out. 我将其用作包含43列和数百行的数据集的文件,我尝试提取特定的列以用作单独的记录,但似乎无法解决。 The error is caused by the final line of code and produces the error message in the title, it works perfectly when the range is set to, stats[30:38] for example. 错误是由代码的最后一行引起的,并在标题中产生错误消息,例如,当范围设置为stats [30:38]时,它可以完美工作。

I have tried storing the required columns in a separate array and calling it like stats[requiredcolumns] but this produces the same error. 我尝试将所需的列存储在单独的数组中,并像stats [requiredcolumns]那样调用它,但这会产生相同的错误。

I have considered using pandas but this is just a small snippet of code from a much larger program, which all functions correctly, and the implementation of pandas would require a complete overhaul of the full program which is not possible due to time constraints. 我已经考虑过使用熊猫,但这只是一个更大程序的一小段代码,所有程序都可以正确运行,而熊猫的实现将需要对整个程序进行彻底检查,由于时间限制,这是不可能的。

Any help would be greatly appreciated 任何帮助将不胜感激

If you have few columns, you can try this: 如果列数很少,可以尝试以下操作:

for line in open(filename):
    stats = line.split(',')
    labels.append(int(stats[0]))
    fvecs.append([float(x) for x in stats[5],stats[6],stats[12],stats[27], stats[29], stats[37], stats[39], stats[41]])

This code will return a list of lists; 这段代码将返回一个列表列表。 otherwise, the first comment is right about indexing and NumpPy. 否则,关于索引和NumpPy的第一条评论是正确的。

暂无
暂无

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

相关问题 TypeError:列表索引必须是整数或切片,而不是元组? - TypeError: list indices must be integers or slices, not tuple? 列表类型错误:列表索引必须是整数或切片,而不是元组 - List of lists TypeError: list indices must be integers or slices, not tuple TypeError:列表索引必须是整数或切片,而不是元组列表的元组 - TypeError: list indices must be integers or slices, not tuple for list of tuples 类型错误:列表索引必须是整数或切片,而不是在 python 中使用 sys 导入的元组 - TypeError: list indices must be integers or slices, not tuple with sys import in python 新编码器:TypeError:列表索引必须是整数或切片,而不是元组 - New coder: TypeError: list indices must be integers or slices, not tuple Python棋盘游戏-“类型错误:列表索引必须是整数或切片,而不是元组” - Python Board Game - "TypeError: list indices must be integers or slices, not tuple" TypeError:列表索引必须是整数或切片,而不是电影分级数据的元组 - TypeError: list indices must be integers or slices, not tuple for Movie Rating Data Python 类型错误:列表索引必须是整数或切片,而不是元组 - Python TypeError: list indices must be integers or slices, not tuple 迭代字典会抛出 TypeError:列表索引必须是整数或切片,而不是元组 - Iterating on dictionary throws TypeError: list indices must be integers or slices, not tuple 如何修复“类型错误:列表索引必须是整数或切片,而不是元组” - How to fix "TypeError: list indices must be integers or slices, not tuple"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM