How can I prevent the TypeError: list indices must be integers, not tuple when copying a python list to a numpy array?

PythonNumpy

Python Problem Overview


I am trying to create 3 numpy arrays/lists using data from another array called mean_data as follows:

---> 39 R = np.array(mean_data[:,0])
     40 P = np.array(mean_data[:,1])
     41 Z = np.array(mean_data[:,2])

When I try run the program I get the error:

TypeError: list indices must be integers, not tuple

The mean_data list looks like this sample...

[6.0, 315.0, 4.8123788544375692e-06],
[6.5, 0.0, 2.259217450023793e-06],
[6.5, 45.0, 9.2823565008402673e-06],
[6.5, 90.0, 8.309270169336028e-06],
[6.5, 135.0, 6.4709418114245381e-05],
[6.5, 180.0, 1.7227922423558414e-05],
[6.5, 225.0, 1.2308522579848724e-05],
[6.5, 270.0, 2.6905672894824344e-05],
[6.5, 315.0, 2.2727114437176048e-05]]

I don't know how to prevent this error, I have tried creating mean_data as a np.array and using np.append to add values to it but that doesn't solve the problem either.

Here's the traceback (was using ipython before)

Traceback (most recent call last):
  File "polarplot.py", line 36, in <module>
    R = np.array(mean_data[:,0])
TypeError: list indices must be integers, not tuple

And the other way I tried to create an array was:

mean_data = np.array([])

for ur, ua in it.product(uradius, uangle):
    samepoints = (data[:,0]==ur) & (data[:,1]==ua)
    if samepoints.sum() > 1:  # check if there is more than one match
        np.append(mean_data[ur, ua, np.mean(data[samepoints,-1])])
    elif samepoints.sum() == 1:
        np.append(mean_data, [ur, ua, data[samepoints,-1]])

The traceback on that is:

IndexError                                Traceback (most recent call last)
<ipython-input-3-5268bc25e75e> in <module>()
     31     samepoints = (data[:,0]==ur) & (data[:,1]==ua)
     32     if samepoints.sum() > 1:  # check if there is more than one match
---> 33         np.append(mean_data[ur, ua, np.mean(data[samepoints,-1])])
     34     elif samepoints.sum() == 1:
     35         np.append(mean_data, [ur, ua, data[samepoints,-1]])

IndexError: invalid index

Python Solutions


Solution 1 - Python

The variable mean_data is a nested list, in Python accessing a nested list cannot be done by multi-dimensional slicing, i.e.: mean_data[1,2], instead one would write mean_data[1][2].

This is becausemean_data[2] is a list. Further indexing is done recursively - since mean_data[2] is a list, mean_data[2][0] is the first index of that list.

Additionally, mean_data[:][0] does not work because mean_data[:] returns mean_data.

The solution is to replace the array ,or import the original data, as follows:

mean_data = np.array(mean_data)

numpy arrays (like MATLAB arrays and unlike nested lists) support multi-dimensional slicing with tuples.

Solution 2 - Python

You probably do not need to be making lists and appending them to make your array. You can likely just do it all at once, which is faster since you can use numpy to do your loops instead of doing them yourself in pure python.

To answer your question, as others have said, you cannot access a nested list with two indices like you did. You can if you convert mean_data to an array before not after you try to slice it:

R = np.array(mean_data)[:,0]

instead of

R = np.array(mean_data[:,0])

But, assuming mean_data has a shape nx3, instead of

R = np.array(mean_data)[:,0]
P = np.array(mean_data)[:,1]
Z = np.array(mean_data)[:,2]

You can simply do

A = np.array(mean_data).mean(axis=0)

which averages over the 0th axis and returns a length-n array

But to my original point, I will make up some data to try to illustrate how you can do this without building any lists one item at a time:

Solution 3 - Python

import numpy as np

mean_data = np.array([
[6.0, 315.0, 4.8123788544375692e-06],
[6.5, 0.0, 2.259217450023793e-06],
[6.5, 45.0, 9.2823565008402673e-06],
[6.5, 90.0, 8.309270169336028e-06],
[6.5, 135.0, 6.4709418114245381e-05],
[6.5, 180.0, 1.7227922423558414e-05],
[6.5, 225.0, 1.2308522579848724e-05],
[6.5, 270.0, 2.6905672894824344e-05],
[6.5, 315.0, 2.2727114437176048e-05]])

R = mean_data[:,0]
print R
print R.shape

EDIT

The reason why you had an invalid index error is the lack of a comma between mean_data and the values you wanted to add.

Also, np.append returns a copy of the array, and does not change the original array. From the documentation :

> Returns : append : ndarray > > A copy of arr with values appended to axis. Note that append does not occur in-place: a new array is allocated and filled. If axis is None, out is a flattened array.

So you have to assign the np.append result to an array (could be mean_data itself, I think), and, since you don't want a flattened array, you must also specify the axis on which you want to append.

With that in mind, I think you could try something like

mean_data = np.append(mean_data, [[ur, ua, np.mean(data[samepoints,-1])]], axis=0)

Do have a look at the doubled [[ and ]] : I think they are necessary since both arrays must have the same shape.

Solution 4 - Python

np.append needs the array as the first argument and the list you want to append as the second:

mean_data = np.append(mean_data, [ur, ua, np.mean(data[samepoints,-1])])

Solution 5 - Python

Just if someone is having this issue and hadn't done list[index, sub-index], you could be having the problem because you're missing a comma between two arrays in an array of arrays (It happened to me).

Solution 6 - Python

Seriously this took my much valuable time but not found solution. Basically if you are taking data from sql then you are converting then you can follow these steps.

rows = cursor.fetchall()---- only tried on single column
dataset=[]
for x in rows:
    dataset.append(float(x[0]))

Solution 7 - Python

Try this :

R = np.array(mean_data[:][0])
P = np.array(mean_data[:][1])
Z = np.array(mean_data[:][2])

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmark mcmurrayView Question on Stackoverflow
Solution 1 - PythonmbattifaranoView Answer on Stackoverflow
Solution 2 - PythonaskewchanView Answer on Stackoverflow
Solution 3 - PythonlmsteffanView Answer on Stackoverflow
Solution 4 - PythongcbirzanView Answer on Stackoverflow
Solution 5 - PythonFabio SungurlianView Answer on Stackoverflow
Solution 6 - PythonKnowledge Software SolutionView Answer on Stackoverflow
Solution 7 - PythonGaurang SolankiView Answer on Stackoverflow