'TypeError: list indices must be integers or slices, not tuple when I try to slice a data read from pickle
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]
Solution 1:[1]
To only get the three first column in each row, you will need something like this :
data = [row[:3] for row in datas]
Solution 2:[2]
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]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Xiidref |
| Solution 2 | FedericoCozziVM |
