Home > Software design >  TypeError: list indices must be integers or slices, not tuple when I try to slice a data read from p
TypeError: list indices must be integers or slices, not tuple when I try to slice a data read from p

Time:05-04

I try to read the pickle data and store it in a variable but when I try to slice it as seen below, it gives a TypeError.

pickle_data = open('data.pickle', 'rb')
datas = pickle.load(pickle_data)

When I print the data, this is the output

output: [[1,2,3,4,5,6,7,8,9], ..., [...]]

I want to read all the rows but not all columns. How can I do that? I tried doing this but it won't work

data = datas[:, :3]

CodePudding user response:

To only get the three first column in each row, you will need something like this :

data = [row[:3] for row in datas]

CodePudding user response:

Seems like you have read a list of lists, so you cannot access with datas[:, :3] Try first to pass it as an numpy array

datas = np.array(datas)
data = datas[:, :3]
  • Related