This question closely relates to How do I run two python loops concurrently?
I'll put it in a clearer manner: I get what the questioner asks in the above link, something like
for i in [1,2,3], j in [3,2,1]:
print i,j
cmp(i,j) #do_something(i,j)
But
L1: for i in [1,2,3] and j in [3,2,1]: doesnt work
Q1. but this was amusing what happened here:
for i in [1,2,3], j in [3,2,1]:
print i,j
[1, 2, 3] 0
False 0
Q2. How do I make something like L1 work?
Not Multithreading or parallelism really. (It's two concurrent tasks not a loop inside a loop) and then compare the result of the two.
Here the lists were numbers. My case is not numbers:
for i in f_iterate1() and j in f_iterate2():
UPDATE: abarnert below was right, I had j defined somewhere. So now it is:
>>> for i in [1,2,3], j in [3,2,1]:
print i,j
Traceback (most recent call last):
File "<pyshell#142>", line 1, in <module>
for i in [1,2,3], j in [3,2,1]:
NameError: name 'j' is not defined
And I am not looking to zip two iteration functions! But process them simultaneously in a for loop like situation. and the question still remains how can it be achieved in python.
UPDATE #2: Solved for same length lists
>>> def a(num):
for x in num:
yield x
>>> n1=[1,2,3,4]
>>> n2=[3,4,5,6]
>>> x1=a(n1)
>>> x2=a(n2)
>>> for i,j in zip(x1,x2):
print i,j
1 3
2 4
3 5
4 6
>>>
[Solved]
Q3. What if n3=[3,4,5,6,7,8,78,34] which is greater than both n1,n2. zip wont work here.something like izip_longest? izip_longest works good enough.