Reputation: 586
Can someone explain this to me.
So, I have a list s
with numbers from -1 to 1 and I want to extract position of certain numbers in a list.
Example:
s= [-1, 0.5, 0.2, -0.9]
z = enumerate(s)
y1 = []
for i,j in z:
if j<=-0.8 and j>=-1:
k = i
y1.append(k)
y2 = []
for i,j in z:
if j<=0.8 and j>=-0.8:
k = i
y2.append(k)
I get y1 = [0, 3]
and y2 = []
But if I define second enumerate
:
z1 = enumerate(s)
y1 = []
for i,j in z1:
if j<=-0.8 and j>=-1:
k = i
y1.append(k)
z2 = enumerate(s)
y2 = []
for i,j in z2:
if j<=0.8 and j>=-0.8:
k = i
y2.append(k)
I get result y1 = [0, 3]
and y2 = [1, 2]
Why do I need second enumerate
?
Upvotes: 3
Views: 710
Reputation: 10951
enumerate
is a lazy generator, once called and traversed all the way it's done. So, I think it's better you include its call directly in the for
loop:
s= [-1, 0.5, 0.2, -0.9]
y1 = []
for i,j in enumerate(s):
if -1 <= j <= -0.8:
y1.append(i)
y2 = []
for i,j in enumerate(s):
if -0.8 <= j <= 0.8:
y2.append(i)
A side note:
if j<=-0.8 and j>=-1:
can be replaced by if -1 <= j <= -0.8:
Upvotes: 2
Reputation: 902
From python docs, the implementation of enumerate is as follow:
def enumerate(collection):
'Generates an indexed series: (0,coll[0]), (1,coll[1]) ...'
i = 0
it = iter(collection)
while 1:
yield (i, it.next())
i += 1
Since it uses 'yield', it is a generator which stores the state when you iterate through it once in line
for i,j in z
To use only one enumerate, convert the values of the generator to a list and reuse the list :
s= [-1, 0.5, 0.2, -0.9]
z = list(enumerate(s))
y1 = []
for i,j in z:
if j<=-0.8 and j>=-1:
k = i
y1.append(k)
y2 = []
for i,j in z:
if j<=0.8 and j>=-0.8:
k = i
y2.append(k)
print(y1)
print(y2)
Output:
[0, 3]
[1, 2]
Upvotes: 2
Reputation: 33691
enumerate
returns an iterator to the sequence. Once traversed, it cannot be used again:
In [1]: l = [1, 2, 3, 4, 5]
In [2]: e = enumerate(l)
In [3]: e
Out[3]: <enumerate at 0x7fad7aea25e8>
In [4]: list(e)
Out[4]: [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]
In [5]: list(e)
Out[5]: []
You could wrap it with a list:
In [6]: e = list(enumerate(l))
In [7]: e
Out[7]: [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]
In [8]: list(e)
Out[8]: [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]
In [9]: list(e)
Out[9]: [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]
You could also use list comprehensions in order to simplify your solution:
y1 = [i for i, x in enumerate(s) if -1 <= x <= -0.8]
y2 = [i for i, x in enumerate(s) if -0.8 <= x <= 0.8]
Upvotes: 3