Echo0831 Echo0831 - 7 months ago 14
Python Question

How to get the element in an array?(python)

array =[[[1, 0.25], [1, 0.19], [1, 0.19]], [[1, 0.89], [0, 0.93], [0, 0.48], [1, 0.49], [0, 0.6], [0, 0.6], [1, 0.34]]]


This array consists of two arrays. I want to get these two. How could I do?
The result should something like this:

sub1=[[1, 0.25], [1, 0.19], [1, 0.19]]
sub2=[[1, 0.89], [0, 0.93], [0, 0.48], [1, 0.49], [0, 0.6], [0, 0.6], [1, 0.34]]


The code is:

for i in array:
print array[i]


Then I got:


TypeError: list indices must be integers, not list

Answer

Your for loop gives the elements in the array not the indices of those elements so you should have:

for item in array:
    print(item)

or to be more similar to what you have written:

for i in range(len(array)):
     print(array[i])

although the first is considered more "Pythonic".

Comments