How about concatenating the two vectors into an array:
for x,y in np.c_[t,s]:
print("xy: %f:%f" % (x,y))
This gives
xy: 0.000000:0.000000
xy: 0.250000:1.000000
xy: 0.500000:0.000000
xy: 0.750000:-1.000000
xy: 1.000000:-0.000000
xy: 1.250000:1.000000
If you want to iterate so you can save memory, you can use the itertools.izip function:
for x,y in itertools.izip(t,s):
print("xy: %f:%f" % (x,y))
Answer from SiggyF on Stack OverflowHow about concatenating the two vectors into an array:
for x,y in np.c_[t,s]:
print("xy: %f:%f" % (x,y))
This gives
xy: 0.000000:0.000000
xy: 0.250000:1.000000
xy: 0.500000:0.000000
xy: 0.750000:-1.000000
xy: 1.000000:-0.000000
xy: 1.250000:1.000000
If you want to iterate so you can save memory, you can use the itertools.izip function:
for x,y in itertools.izip(t,s):
print("xy: %f:%f" % (x,y))
for x, y in zip(t,s):. For 1d arrays, it's really that simple.
Verified to work in both Python 2 and Python 3. zip() returns a list on Python2 though, so as DiggyF suggests, itertools.izip() may be more appropriate for large arrays.
For >1D arrays, iteration moves through the last dimension returning (N-1)D arrays. If you have to deal with N-d arrays, this may or may not be what you want.
Regardless, it's unquestionably portable, and iterating on array objects is intended to support this kind of usecase :)
How about concatenating the two vectors into an array:
for x,y in np.c_[t,s]:
print("xy: %f:%f" % (x,y))
This gives
xy: 0.000000:0.000000
xy: 0.250000:1.000000
xy: 0.500000:0.000000
xy: 0.750000:-1.000000
xy: 1.000000:-0.000000
xy: 1.250000:1.000000
If you want to iterate so you can save memory, you can use the itertools.izip function:
for x,y in itertools.izip(t,s):
print("xy: %f:%f" % (x,y))
for x, y in zip(t,s):. For 1d arrays, it's really that simple.
Verified to work in both Python 2 and Python 3. zip() returns a list on Python2 though, so as DiggyF suggests, itertools.izip() may be more appropriate for large arrays.
For >1D arrays, iteration moves through the last dimension returning (N-1)D arrays. If you have to deal with N-d arrays, this may or may not be what you want.
Regardless, it's unquestionably portable, and iterating on array objects is intended to support this kind of usecase :)
If the lists a and b are short, use zip (as @Vincenzo Pii showed):
for x, y in zip(a, b):
print(x + y)
If the lists a and b are long, then use itertools.izip to save memory:
import itertools as IT
for x, y in IT.izip(a, b):
print(x + y)
zip creates a list of tuples. This can be burdensome (memory-wise) if a and b are large.
itertools.izip returns an iterator. The iterator does not generate the complete list of tuples; it only yields each item as it is requested by the for-loop. Thus it can save you some memory.
In Python2 calling zip(a,b) on short lists is quicker than using itertools.izip(a,b). But in Python3 note that zip returns an iterator by default (i.e. it is equivalent to itertools.izip in Python2).
Other variants of interest:
- from future_builtin import zip -- if you wish to program with
Python3-style
zipwhile in Python2. - itertools.izip_longest -- if
aandbare of unequal length.
A possible solution is using zip, as you mentioned yourself, but slightly differently than how you wrote it in the question:
for x, y in zip(a, b):
print x, y
Notice that the length of the list of tuples returned by zip() will be equal to the minimum between the lengths of a and b. This impacts when a and b are not of the same length.