Reputation: 13
I'm trying to sort through a list of lists. How could I go about printing or iterating over the first and last elements in each list? non-working code below:
from numpy import *
xs=[[1.,2.,3.],[4.,5.,6.],[7.,8.,9.]]
for i in xs:
for j in xs[i]:
print(xs[1],xs[-1])
Traceback error if needed:
runfile('/Users/Alex/untitled9.py', wdir='/Users/Alex')
Traceback (most recent call last):
File "<ipython-input-14-8a28382c7f81>", line 1, in <module>
runfile('/Users/Alex/untitled9.py', wdir='/Users/Alex')
File "/anaconda/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 880, in runfile
execfile(filename, namespace)
File "/anaconda/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/Users/Alex/untitled9.py", line 13, in <module>
for j in xs[i]:
TypeError: list indices must be integers or slices, not list
Upvotes: 1
Views: 7213
Reputation: 403198
You've already been given the answer in the comments. But to understand why your code doesn't work in its current state, take a look at this:
>>> xs = [[1.,2.,3.], [4.,5.,6.], [7.,8.,9.]]
>>> for i in xs:
... print(i)
...
[1.0, 2.0, 3.0]
[4.0, 5.0, 6.0]
[7.0, 8.0, 9.0]
The for
loop iterates over the elements, not the indices. The variable i
is probably confusing here. Anyway, inside the loop, i
will contain a sublist at each iteration. So xs[i]
is an invalid operation here -- you may only provide integers as indices to a python list.
If you want to get the first and last element of each sublist, all you've got to do is print out i[0]
and i[-1]
.
On a related note, you can iterate over the indices using the range
function:
for i in range(len(xs)):
print(xs[i][0], xs[i][-1])
But this is not recommended, since it is more efficient to just iterate over the elements directly, especially for this use case.
You can also also use enumerate
, if you need both:
for c, i in enumerate(xs):
print(i[0], xs[c][-1]) # both work here
Upvotes: 6
Reputation: 648
Try this:
li = []
for i in range(len(xs)):
li.append([xs[i][0],xs[i][-1]])
output: [[1.0, 3.0], [4.0, 6.0], [7.0, 9.0]]
Upvotes: 0
Reputation: 49
When you iterate over a list you get an item from each member of the list, and if that item is a list you can create a second loop to iterate over the item, like this:
for items in xs:
for elem in items:
# in here you get each element of the inner list
To get the first and last value though, You don't need that you can as answered previously:
for item in xs:
print(item[0], item[-1]
And if you are feeling a little funny you could even store those in your own list by using a comprehensive list, like so:
funny_list= [[item[0], item[-1]] for item in xs]
Upvotes: 0
Reputation: 664
In the code you have given:
for i in xs:
for j in xs[i]:
print(xs[1],xs[-1])
in the outer for loop, the variable i
stores the inner arrays, so in the inner for loop, when you do xs[i]
, it gives an error. This is because you can only use integers as indices in an array (which makes sense, since you would want the the 0th or 1st or 2nd element, there is no such thing like [1,2,3]th element, which xs[i]
in the code you have written essentially means).
Now, in order to simply iterate over the multidimensional array:
for i in xs:
for j in i:
print(j)
print("\n")
This will give:
[1.0, 2.0, 3.0]
[4.0, 5.0, 6.0]
[7.0, 8.0, 9.0]
If you want to print only the first and last items of the inner lists, this would do:
for i in xs:
print(i[0], i[-1], "\n")
Hope this helps
Upvotes: 0