You can use zip with more than one iterable, as well: > s = 'abc' In Python 3, you get a “zip object” back.) The tuple at index 0 contains s and t. Before I describe what “zip” does, let me first show you an example: > s = 'abc'Īs you can see, the result of “zip” is a sequence of tuples. I’m not sure just what it is about zip that I enjoy, but I have often found it to be quite useful. Introduction to machine learning in Python.Note that we don't actually have the tuple Use the * operator is an iterator that will yield (1,1) then (2,2) up Yeah, the difference is a little subtle here. Could you expand on what you mean by exhaust the iterators too early? Satchidanand Haridas wrote in message news. I am trying to understand what goes on inside izip myself. Simply converts the result of the inverse izip operation which into an So the operation of the itertools.izip(range10(),range10()) is completedĪnd "exhausted" is printed before the * operation is applied. When the iterator isĬalled the 11th time, it prints "exhausted". Times, each time returning (i,i) for 0<=0<10. Actually to the iterator which is called 10 The reason I ask is that the * operator is applied to the tuple I am trying to understand a little about the izip myself. And the iter() simply converts the result of the inverse Itertools.izip(range10(),range10()) is completed before the * operation The reason I ask is that the * operator is applied to > list(m)Ĭould you expand on what you mean by exhaust the iterators too early? Return (cut(itr, index) for (index, itr) in enumerate(t))Ī, b, c = starzip(it.izip("abc",, "xyz")) "starzip() does not allow an empty sequence as argument") Repeating your demo, you are getting the same (last) sequence twice due to However, your sample data is badly chosen. Single line solution if I wasn't using 2.4.)īecause Python supports function definitions you only have to do it once :-) Seems like a bit of work for the inverse of izip though so I'll wait toĪnyone else has a better solution. Starzip = lambda iterables: ((tuple for tuple in itr) for i, itr in enumerate(itertools.tee(iterables))) starzip(itertools.izip(range(10), range(10))) x, y = starzip(itertools.izip(range(10), range(10))) ![]() > x, y = itertools.izip(*itertools.izip(range(10), range(10))) Me like all the pairs from the first iterator are read into memory before the But we are hitting "exhausted"īefore we ever ask for an element from the starzip2 iterators, so it looks to Up with izip, the next iterator is discarded. I believe we only get one "exhausted" because as soon as one iterator is used ![]() Unfortunately, I think this exhausts the iterators too early because it ![]() > starzip2 = lambda it: tuple() > l,m = starzip2(itertools.izip(range(10),range(10))) > l > m ![]() Satchidanand Haridas > writes: How about using iter() to get another solution like the following: X, y = starzip(itertools.izip(range(10), range(10))) Starzip(itertools.izip(range(10), range(10))) >x, y = itertools.izip(*itertools.izip(range(10), range(10))) How about using iter() to get another solution like the following: Seems like a bit of work for the inverse of izip though so I'll wait to see ifĪnyone else has a better solution. x, y = starzip(itertools.izip(range(10), range(10))) Starzip = lambda iterables: ((tuple for tuple in itr) for i, itr inĮnumerate(itertools.tee(iterables))) starzip(itertools.izip(range(10), range(10))) Sorry to respond to myself, but after playing around with itertools for a Steven Bethard > writes: What's the inverse of izip? Of course, I could use zip(*) or izip(*), Satchidanand Haridas (sharidas at zeomega dot com) I want to convert an iterator of tuples into a tuple of iterators.Ī = itertools.izip(*itertools.izip(range(10),range(10) )) X, y = itertools.izip(*itertools.izip(range(10), range(10))) What's the inverse of izip? Of course, I could use zip(*) or izip(*), So I know that zip(*) is the inverse of zip(), e.g.:
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |