TypeError:list indices必须是整数,而不是元组

时间:2021-09-05 17:08:24

My code:`

#!/usr/bin/python

with open("a.dat", "r") as ins:
    array = []
    for line in ins:
        l=line.strip()
        array.append(l)

a1 = array[:,1]
print a1

I want to read a.dat as array then take the first column.What is wrong?

我想读取a.dat作为数组,然后取第一列。出了什么问题?

2 个解决方案

#1


For loading numerical data, it's often useful to use numpy instead of just Python.

对于加载数值数据,使用numpy而不仅仅是Python通常很有用。

import numpy as np
arr = np.loadtxt('a.dat')
print arr[:,0]

numpy is a Python library that's very well suited to loading and manipulating numerical data (with a bonus that when used correctly, it's waaaay faster than using Python lists). In addition, for dealing with tabular data with mixed datatypes, I recommend using pandas.

numpy是一个非常适合加载和操作数值数据的Python库(如果使用得当,它比使用Python列表更快)。另外,为了处理具有混合数据类型的表格数据,我建议使用pandas。

import pandas as pd
df = pd.load_csv('a.dat', sep=' ', names=['col1', 'col2'])
print df['col1']

Numpy can be found here

Numpy可以在这里找到

Pandas can be found here

熊猫可以在这里找到

#2


This is wrong: a1 = array[:,1] putting values separated with comma make it a Tuple of 2 values. You should use:

这是错误的:a1 = array [:,1]将值用逗号分隔,使其成为2个值的元组。你应该使用:

a1 = array[0]

To get first row or to get first column use:

要获得第一行或获得第一列使用:

column = [row[0] for row in array]

#1


For loading numerical data, it's often useful to use numpy instead of just Python.

对于加载数值数据,使用numpy而不仅仅是Python通常很有用。

import numpy as np
arr = np.loadtxt('a.dat')
print arr[:,0]

numpy is a Python library that's very well suited to loading and manipulating numerical data (with a bonus that when used correctly, it's waaaay faster than using Python lists). In addition, for dealing with tabular data with mixed datatypes, I recommend using pandas.

numpy是一个非常适合加载和操作数值数据的Python库(如果使用得当,它比使用Python列表更快)。另外,为了处理具有混合数据类型的表格数据,我建议使用pandas。

import pandas as pd
df = pd.load_csv('a.dat', sep=' ', names=['col1', 'col2'])
print df['col1']

Numpy can be found here

Numpy可以在这里找到

Pandas can be found here

熊猫可以在这里找到

#2


This is wrong: a1 = array[:,1] putting values separated with comma make it a Tuple of 2 values. You should use:

这是错误的:a1 = array [:,1]将值用逗号分隔,使其成为2个值的元组。你应该使用:

a1 = array[0]

To get first row or to get first column use:

要获得第一行或获得第一列使用:

column = [row[0] for row in array]