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

0 votes

I want 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])

I get this error when I run this code:

TypeError: list indices must be integers, not tuple

 I have tried creating mean_data as an np.array and using np.append to add values to it but that doesn't solve the problem either.

Here's the traceback:

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

This is how I tried creating an array:

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]])

This is the the traceback on that :

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

Can someone guide me in solving this?

May 7, 2022 in Python by narikkadan
• 63,420 points
605 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Python

0 votes
3 answers
+1 vote
1 answer

Can anyone help me with this error: TypeError: list indices must be integers or slices, not str

Hi, @Varshap  It’s a TypeError, which tells us ...READ MORE

answered Nov 5, 2020 in Python by anonymous
• 65,910 points
2,047 views
0 votes
1 answer

i am getting this error "TypeError: tuple indices must be integers or slices, not Database"

Hi, @Shabaj, Regarding your query, I hope this ...READ MORE

answered Dec 10, 2020 in Python by Gitika
• 65,910 points
904 views
0 votes
0 answers
0 votes
2 answers

How can I get the count of a list in Python?

n=[1,2,3,4,5,6,7,8,9] print(len(n)) =9 READ MORE

answered Dec 10, 2020 in Python by anonymous
1,184 views
+1 vote
2 answers

View onto a numpy array?

 just index it as you normally would. ...READ MORE

answered Oct 18, 2018 in Python by roberto
696 views
0 votes
1 answer
0 votes
1 answer

Printing a large numpy array

numpy.set_printoptions(threshold='nan') READ MORE

answered Jul 20, 2018 in Python by Nietzsche's daemon
• 4,260 points
1,547 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP