Posts: 4,647
Threads: 1,494
Joined: Sep 2016
Mar-23-2017, 04:13 AM
(This post was last modified: Mar-23-2017, 06:13 AM by Kebap.)
i had been thinking this might be a nice feature but i am wondering if this could be done with a generator. the original idea was:
for a,b,c in [1,2],['three','four'],['foo','bar']:
print(a,b,c) Output: 1 three foo
2 four bar
it would be parallel iteration over 3 separate lists of the same length. if the lengths differ iteration stops at the end of the shortest one.
could this be done with any iterator?
Moderator Kebap: Moved thread to General Coding
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 470
Threads: 29
Joined: Sep 2016
Do you know python zip()?
Posts: 4,647
Threads: 1,494
Joined: Sep 2016
there i go again, not knowing the right words to search, although i didn't do it recently. i guess it is best for each thing i need to do in python is assume it has already been done. i guess i got this from C experience, where almost nothing had been done, and from assembler 360 experience where there was even less.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 11
Threads: 1
Joined: Mar 2017
The length of each list should be equal to or more than the no of values we provide (a,b,c...)
for a,b,c in [1,2,3],[4,5,6],[7,8,9]:
print (a,b,c)
the output is:
(1, 2, 3)
(4, 5, 6)
(7, 8, 9)
this is no need to use same data type ,any data type can be used as list member.
Posts: 4,647
Threads: 1,494
Joined: Sep 2016
Mar-24-2017, 05:38 AM
(This post was last modified: Mar-24-2017, 05:38 AM by Skaperen.)
(Mar-23-2017, 08:53 AM)ankit Wrote: The length of each list should be equal to or more than the no of values we provide (a,b,c...)
for a,b,c in [1,2,3],[4,5,6],[7,8,9]:
print (a,b,c)
the output is:
(1, 2, 3)
(4, 5, 6)
(7, 8, 9)
this is no need to use same data type ,any data type can be used as list member.
what i am wanting is for the 3 lists to be iterated in parallel so that the first iteration gets [1,4,7]. Output: lt1/forums /home/forums 11> py3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> for x in zip([1,2,3],[4,5,6],[7,8,9]):
... print(x)
...
(1, 4, 7)
(2, 5, 8)
(3, 6, 9)
>>>
lt1/forums /home/forums 12>
this is exactly what i was looking for. the name probably comes from the action of a zipper for the case of 2 iterators. this will let me iterate over N lists in parallel without using indexes or constructing a list of N-tuples.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 4,647
Threads: 1,494
Joined: Sep 2016
Mar-24-2017, 06:45 AM
(This post was last modified: Mar-24-2017, 06:45 AM by Skaperen.)
i wrote a little script using zip() ... not what i was originally thinking about this for. it is in Scripts & Snippets.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 8,160
Threads: 160
Joined: Sep 2016
Mar-24-2017, 06:51 AM
(This post was last modified: Mar-24-2017, 07:38 AM by buran.)
Just for sake of completeness and to the benefit of Python2 users - in Python2 zip returns list, while in itertools.izip() returns iterator. In Python3 zip returns iterator and itertools.izip() is not available.
Also Python2 has itertools.izip_longest () and in Python3 there is itertools.zip_longest() to process iterables of unequal size and option to provide a fill value for shorter one(s).
|