473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

iterator question

Any suggestions for transforming the sequence:

[1, 2, 3, 4...]
Where 1,2,3.. are it the ith item in an arbitrary sequence

into a succession of tuples:

[(1, 2), (3, 4)...]

In other words, given a seq and an integer that specifies the size of tuple
to return, then for example:

seq = [a,b,c,d,e,f]
for e in transform (seq, 2):
print e

would return
(a,b)
(c,d)
(e,f)

Sep 26 '06 #1
14 1242
def transform(seq, size):
i = 0
while i < len(seq):
yield tuple(seq[i:i+size])
i += size

Neal Becker wrote:
Any suggestions for transforming the sequence:

[1, 2, 3, 4...]
Where 1,2,3.. are it the ith item in an arbitrary sequence

into a succession of tuples:

[(1, 2), (3, 4)...]

In other words, given a seq and an integer that specifies the size of tuple
to return, then for example:

seq = [a,b,c,d,e,f]
for e in transform (seq, 2):
print e

would return
(a,b)
(c,d)
(e,f)
Sep 26 '06 #2

Neal Becker wrote:
Any suggestions for transforming the sequence:

[1, 2, 3, 4...]
Where 1,2,3.. are it the ith item in an arbitrary sequence

into a succession of tuples:

[(1, 2), (3, 4)...]

In other words, given a seq and an integer that specifies the size of tuple
to return, then for example:

seq = [a,b,c,d,e,f]
for e in transform (seq, 2):
print e

would return
(a,b)
(c,d)
(e,f)
How about this:

def transform(seq,n):
for k in xrange(0,len(seq),n):
yield tuple(seq[k:k+n])

Sep 26 '06 #3
jo********@gmail.com wrote:
def transform(seq, size):
i = 0
while i < len(seq):
yield tuple(seq[i:i+size])
i += size
Or for arbitrary iterables, not just sequences:

from itertools import islice
def transform(iterable, size):
it = iter(iterable)
while True:
window = tuple(islice(it,size))
if not window:
break
yield window

George

Sep 26 '06 #4
On 2006-09-26, Neal Becker <nd*******@gmail.comwrote:
Any suggestions for transforming the sequence:

[1, 2, 3, 4...]
Where 1,2,3.. are it the ith item in an arbitrary sequence

into a succession of tuples:

[(1, 2), (3, 4)...]

In other words, given a seq and an integer that specifies the
size of tuple to return, then for example:
It turns out there's a itertools recipe to do this; the last one
in the itertools recipe book:

def grouper(n, iterable, padvalue=None):
"""
grouper(3, 'abcdefg', 'x') --('a','b','c'), ('d','e','f'), ('g','x','x')

"""
return izip(*[chain(iterable, repeat(padvalue, n-1))]*n)

--
Neil Cerutti
There are two ways to argue with a woman, and neither of them
work. --Carlos Boozer
Sep 26 '06 #5
Neil Cerutti wrote:
On 2006-09-26, Neal Becker <nd*******@gmail.comwrote:
Any suggestions for transforming the sequence:

[1, 2, 3, 4...]
Where 1,2,3.. are it the ith item in an arbitrary sequence

into a succession of tuples:

[(1, 2), (3, 4)...]

In other words, given a seq and an integer that specifies the
size of tuple to return, then for example:

It turns out there's a itertools recipe to do this; the last one
in the itertools recipe book:

def grouper(n, iterable, padvalue=None):
"""
grouper(3, 'abcdefg', 'x') --('a','b','c'), ('d','e','f'), ('g','x','x')

"""
return izip(*[chain(iterable, repeat(padvalue, n-1))]*n)
That's not quite the same as the previous suggestions; if the last
tuple is shorter than n, it pads the last tuple with padvalue. The OP
didn't mention if he wants that or he'd rather have a shorter last
tuple.

George

Sep 26 '06 #6
Neal Becker wrote:
Any suggestions for transforming the sequence:

[1, 2, 3, 4...]
Where 1,2,3.. are it the ith item in an arbitrary sequence

into a succession of tuples:

[(1, 2), (3, 4)...]

In other words, given a seq and an integer that specifies the size of tuple
to return, then for example:
>>trf = lambda iterable,n : zip(*[iter(iterable)]*n)
trf(range(15),3)
[(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11), (12, 13, 14)]
note though that it will silently drop any remainder.

hth, bb
Sep 26 '06 #7
Neal Becker a écrit :
Any suggestions for transforming the sequence:

[1, 2, 3, 4...]
Where 1,2,3.. are it the ith item in an arbitrary sequence

into a succession of tuples:

[(1, 2), (3, 4)...]

In other words, given a seq and an integer that specifies the size of tuple
to return, then for example:

seq = [a,b,c,d,e,f]
for e in transform (seq, 2):
print e

would return
(a,b)
(c,d)
(e,f)
Just for the fun (I'm sure someone already posted a far better solution):

def transform(seq, step):
return [tuple(seq[x: x+step]) \
for x in range(0, len(seq)-(step -1), step)]
Sep 26 '06 #8
George Sakkis wrote:
Neil Cerutti wrote:

>>On 2006-09-26, Neal Becker <nd*******@gmail.comwrote:
>>>Any suggestions for transforming the sequence:

[1, 2, 3, 4...]
Where 1,2,3.. are it the ith item in an arbitrary sequence

into a succession of tuples:

[(1, 2), (3, 4)...]

In other words, given a seq and an integer that specifies the
size of tuple to return, then for example:

It turns out there's a itertools recipe to do this; the last one
in the itertools recipe book:

def grouper(n, iterable, padvalue=None):
"""
grouper(3, 'abcdefg', 'x') --('a','b','c'), ('d','e','f'), ('g','x','x')

"""
return izip(*[chain(iterable, repeat(padvalue, n-1))]*n)


That's not quite the same as the previous suggestions; if the last
tuple is shorter than n, it pads the last tuple with padvalue. The OP
didn't mention if he wants that or he'd rather have a shorter last
tuple.
In which case why not go in for a bit of requirements gold-plating and
add a keyword Boolean argument that allows you to specify which
behaviour you want. Or, alternatively, let the OP make sense of the
suggestions already made.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Sep 26 '06 #9
Neal Becker wrote in
news:ma**************************************@pyth on.org in
comp.lang.python:
Any suggestions for transforming the sequence:

[1, 2, 3, 4...]
Where 1,2,3.. are it the ith item in an arbitrary sequence

into a succession of tuples:

[(1, 2), (3, 4)...]

In other words, given a seq and an integer that specifies the size of
tuple to return, then for example:

seq = [a,b,c,d,e,f]
for e in transform (seq, 2):
print e

would return
(a,b)
(c,d)
(e,f)
>>seq = range(11)
zip(seq[::2], seq[1::2] + [None])
[(0, 1), (2, 3), (4, 5), (6, 7), (8, 9), (10, None)]
>>seq = range(10)
zip(seq[::2], seq[1::2] + [None])
[(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)]
Rob.
--
http://www.victim-prime.dsl.pipex.com/
Sep 26 '06 #10
Rob Williscroft wrote in news:Xns984ACDA635C9rtwfreenetREMOVEcouk@
216.196.109.145 in comp.lang.python:
>>>seq = range(11)
zip(seq[::2], seq[1::2] + [None])
[(0, 1), (2, 3), (4, 5), (6, 7), (8, 9), (10, None)]
>>>seq = range(10)
zip(seq[::2], seq[1::2] + [None])
[(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)]
For bigger sequences:
>>import itertools as it
seq = range(11)
even = it.islice( seq, 0, None, 2 )
odd = it.islice( seq, 1, None, 2 )
zipped = it.izip( even, it.chain( odd, [None] ) )
list( zipped )
[(0, 1), (2, 3), (4, 5), (6, 7), (8, 9), (10, None)]
Rob.
--
http://www.victim-prime.dsl.pipex.com/
Sep 26 '06 #11
Steve Holden wrote:
George Sakkis wrote:
Neil Cerutti wrote:

>On 2006-09-26, Neal Becker <nd*******@gmail.comwrote:

Any suggestions for transforming the sequence:

[1, 2, 3, 4...]
Where 1,2,3.. are it the ith item in an arbitrary sequence

into a succession of tuples:

[(1, 2), (3, 4)...]

In other words, given a seq and an integer that specifies the
size of tuple to return, then for example:

It turns out there's a itertools recipe to do this; the last one
in the itertools recipe book:

def grouper(n, iterable, padvalue=None):
"""
grouper(3, 'abcdefg', 'x') --('a','b','c'), ('d','e','f'), ('g','x','x')

"""
return izip(*[chain(iterable, repeat(padvalue, n-1))]*n)

That's not quite the same as the previous suggestions; if the last
tuple is shorter than n, it pads the last tuple with padvalue. The OP
didn't mention if he wants that or he'd rather have a shorter last
tuple.
In which case why not go in for a bit of requirements gold-plating and
add a keyword Boolean argument that allows you to specify which
behaviour you want.
Ok, I'll bite. Here's an overgeneralized, itertools-infested
conglomerate of all the suggestions so far. Season to taste:

from itertools import islice,izip,chain,repeat,takewhile,count

def iterwindows(iterable, n=2, mode='keep', padvalue=None):
it = iter(iterable)
if mode == 'keep':
return takewhile(bool, (tuple(islice(it,n)) for _ in count()))
elif mode == 'drop':
return izip(*[it]*n)
elif mode == 'pad':
return izip(*[chain(it,repeat(padvalue,n-1))]*n)
else:
raise ValueError('Unknown mode: %r' % mode)
>>list(iterwindows('abcdefgh',3))
[('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'h')]
>>list(iterwindows('abcdefgh',3,mode='drop'))
[('a', 'b', 'c'), ('d', 'e', 'f')]
list(iterwindows('abcdefgh',3,mode='pad'))
[('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'h', None)]
George

Sep 26 '06 #12
George Sakkis wrote:
jo********@gmail.com wrote:
>def transform(seq, size):
i = 0
while i < len(seq):
yield tuple(seq[i:i+size])
i += size

Or for arbitrary iterables, not just sequences:

from itertools import islice
def transform(iterable, size):
it = iter(iterable)
while True:
window = tuple(islice(it,size))
if not window:
break
yield window

George
Thanks guys!

This one above is my personal favorite.
Sep 27 '06 #13
Simple list comprehension?
>>l = [1,2,3,4,5,6,7,8,9,0]
[(x, x+1) for x in l if x%2 == 1]
[(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]

Danny

Neal Becker wrote:
George Sakkis wrote:
jo********@gmail.com wrote:
def transform(seq, size):
i = 0
while i < len(seq):
yield tuple(seq[i:i+size])
i += size
Or for arbitrary iterables, not just sequences:

from itertools import islice
def transform(iterable, size):
it = iter(iterable)
while True:
window = tuple(islice(it,size))
if not window:
break
yield window

George

Thanks guys!

This one above is my personal favorite.
Sep 27 '06 #14
Er, whoops. That would work if the last item in the list was 10 (and,
of course, this doesn't work for any arbitrary sequence). Is there any
"look-ahead" function for list comprehensions?

Danny

da***********@gmail.com wrote:
Simple list comprehension?
>l = [1,2,3,4,5,6,7,8,9,0]
[(x, x+1) for x in l if x%2 == 1]
[(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]

Danny

Neal Becker wrote:
George Sakkis wrote:
jo********@gmail.com wrote:
>
>def transform(seq, size):
> i = 0
> while i < len(seq):
> yield tuple(seq[i:i+size])
> i += size
>
Or for arbitrary iterables, not just sequences:
>
from itertools import islice
def transform(iterable, size):
it = iter(iterable)
while True:
window = tuple(islice(it,size))
if not window:
break
yield window
>
George
>
Thanks guys!

This one above is my personal favorite.
Sep 27 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages...
26
by: Michael Klatt | last post by:
I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code: template <class Set> // Set is an instance of std::set<> class...
5
by: music4 | last post by:
Greetings, I want to STL map class. But I don't know how to use iterator. Let me state my question by following small sample: map<int, int> mymap; // insert some <key,value> in mymap ...
11
by: Mateusz Loskot | last post by:
Hi, I have a simple question about naming convention, recommeded names or so in following case. I have a class which is implemented with one of STL container (aggregated) and my aim is to...
8
by: Mateusz Åoskot | last post by:
Hi, I know iterator categories as presented by many authors: Stroustrup, Josuttis and Koenig&Moo: Input <---| |<--- Forward <--- Bidirectional <--- Random Output <---|
5
by: Mark Stijnman | last post by:
I have a question about forward iterators and what one should do or not do with them. I'm planning on writing a container that, when all boils down to it, stores a sequence of strings. I want...
3
by: wolverine | last post by:
Hi I am accessing a map from inside threads. There is a chance that an element is inserted into the map, from inside any thread. Since i don't know about thread safety of stl implementation i am...
3
by: Alex__655321 | last post by:
Hello All I hope I'm correct posting an STL question here - if not feel free to direct me to somewhere more appropriate. I'm writing some code using an std::set which I believe is the best...
16
by: Juha Nieminen | last post by:
I'm actually not sure about this one: Does the standard guarantee that if there's at least one element in the data container, then "--container.end()" will work and give an iterator to the last...
2
by: Terry Reedy | last post by:
Luis Zarrabeitia wrote: Interesting observation. Iterators are intended for 'iterate through once and discard' usages. To zip a long sequence with several short sequences, either use...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.