I'm trying to learn neat pythonic ways of doing things, and was wondering why my for loop cannot be refactored this way:
q = [1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5]
vm = [-1, -1, -1, -1]
for v in vm:
if v in q:
p.append(q.index(v))
else:
p.append(99999)
vm[p.index(max(p))] = i
I tried replacing the for loop with:
[p.append(q.index(v)) if v in q else p.append(99999) for v in vm]
But it doesn't work. The for v in vm:
loop evicts numbers from vm
based on when they come next in q
.
This question is related to
python
syntax
list-comprehension
q = [1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5]
vm = [-1, -1, -1, -1,1,2,3,1]
p = []
for v in vm:
if v in q:
p.append(q.index(v))
else:
p.append(99999)
print p
p = [q.index(v) if v in q else 99999 for v in vm]
print p
Output:
[99999, 99999, 99999, 99999, 0, 1, 2, 0]
[99999, 99999, 99999, 99999, 0, 1, 2, 0]
Instead of using append()
in the list comprehension you can reference the p as direct output, and use q.index(v)
and 99999
in the LC.
Not sure if this is intentional but note that q.index(v)
will find just the first occurrence of v
, even tho you have several in q
. If you want to get the index of all v
in q
, consider using a enumerator
and a list of already visited indexes
Something in those lines(pseudo-code):
visited = []
for i, v in enumerator(vm):
if i not in visited:
p.append(q.index(v))
else:
p.append(q.index(v,max(visited))) # this line should only check for v in q after the index of max(visited)
visited.append(i)
your list comphresnion will, work but will return list of None because append return None:
demo:
>>> a=[]
>>> [ a.append(x) for x in range(10) ]
[None, None, None, None, None, None, None, None, None, None]
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
better way to use it like this:
>>> a= [ x for x in range(10) ]
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Source: Stackoverflow.com