Re: slice iterator?

2009-05-09 Thread Emile van Sebille

On 5/8/2009 8:17 PM Ross said...

I have a really long list that I would like segmented into smaller
lists. Let's say I had a list a = [1,2,3,4,5,6,7,8,9,10,11,12] and I
wanted to split it into groups of 2 or groups of 3 or 4, etc. Is there
a way to do this without explicitly defining new lists? 


>>> a = [1,2,3,4,5,6,7,8,9,10,11,12]
>>>
>>> for k in [2,3,4,5,6]:
... print [ a[j:j+k] for j in range(0,len(a),k) ]
...
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]]
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12]]
[[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]]

The point is of course to use the form a[j:j+k] which doesn't create new 
lists.


Emile

--
http://mail.python.org/mailman/listinfo/python-list


Re: slice iterator?

2009-05-09 Thread ryles
On May 8, 11:17 pm, Ross  wrote:
> I have a really long list that I would like segmented into smaller
> lists. Let's say I had a list a = [1,2,3,4,5,6,7,8,9,10,11,12] and I
> wanted to split it into groups of 2 or groups of 3 or 4, etc. Is there
> a way to do this without explicitly defining new lists? If the above
> problem were to be split into groups of 3, I've tried something like:
>
> start = 0
> stop = 3
> for i in range(len(a)):
>     segment = a[start:stop]
>     print segment
>     start += stop
>     stop += stop
>
> Unfortunately start and stop don't increment using this code. Ideally,
> my outcome would be
> [1,2,3]
> [4,5,6]
> [7,8,9]
> [10,11,12]

There is also an iterator version here:
http://code.activestate.com/recipes/303279
--
http://mail.python.org/mailman/listinfo/python-list


Re: slice iterator?

2009-05-08 Thread Paul McGuire
On May 8, 11:14 pm, Ned Deily  wrote:
> In article <7xprejoswg@ruckus.brouhaha.com>,
>  Paul Rubin  wrote:
>
>
>
>
>
> > Ross  writes:
> > > I have a really long list that I would like segmented into smaller
> > > lists. Let's say I had a list a = [1,2,3,4,5,6,7,8,9,10,11,12] and I
> > > wanted to split it into groups of 2 or groups of 3 or 4, etc. Is there
> > > a way to do this without explicitly defining new lists?
>
> > That question comes up so often it should probably be a standard
> > library function.
>
> > Anyway, here is an iterator, if that's what you want:
> >    >>> from itertools import islice
> >    >>> a = range(12)
> >    >>> xs = iter(lambda x=iter(a): list(islice(x,3)), [])
> >    >>> print list(xs)
> >    [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]]
> > Of course, as the saying goes, there's more than one way to do it ;-)
>
> python2.6 itertools introduces the izip_longest function and the grouper
> recipe :
>
> def grouper(n, iterable, fillvalue=None):
>     "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
>     args = [iter(iterable)] * n
>     return izip_longest(fillvalue=fillvalue, *args)
>
> --
>  Ned Deily,
>  n...@acm.org- Hide quoted text -
>
> - Show quoted text -

Here's a version that works pre-2.6:

>>> grouper = lambda iterable,size,fill=None : 
>>> zip(*[(iterable+[fill,]*(size-1))[i::size] for i in range(size)])
>>> a = range(12)
>>> grouper(a,6)
[(0, 1, 2, 3, 4, 5), (6, 7, 8, 9, 10, 11)]
>>> grouper(a,5)
[(0, 1, 2, 3, 4), (5, 6, 7, 8, 9), (10, 11, None, None, None)]
>>> grouper(a,3)
[(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11)]

-- Paul
--
http://mail.python.org/mailman/listinfo/python-list


Re: slice iterator?

2009-05-08 Thread Scott David Daniels

Ross wrote:

I have a really long list that I would like segmented into smaller
lists. Let's say I had a list a = [1,2,3,4,5,6,7,8,9,10,11,12] and I
wanted to split it into groups of 2 or groups of 3 or 4, etc. Is there
a way to do this without explicitly defining new lists? If the above
problem were to be split into groups of 3, I've tried something like:
... Ideally, my outcome would be
[1,2,3]
[4,5,6]
[7,8,9]
[10,11,12]


for i in range(0, len(a), 3):
segment = a[i : i +3]
print segment

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: slice iterator?

2009-05-08 Thread Ned Deily
In article <7xprejoswg@ruckus.brouhaha.com>,
 Paul Rubin  wrote:
> Ross  writes:
> > I have a really long list that I would like segmented into smaller
> > lists. Let's say I had a list a = [1,2,3,4,5,6,7,8,9,10,11,12] and I
> > wanted to split it into groups of 2 or groups of 3 or 4, etc. Is there
> > a way to do this without explicitly defining new lists? 
> 
> That question comes up so often it should probably be a standard
> library function.
> 
> Anyway, here is an iterator, if that's what you want:
>>>> from itertools import islice
>>>> a = range(12)
>>>> xs = iter(lambda x=iter(a): list(islice(x,3)), [])
>>>> print list(xs)
>[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]]
> Of course, as the saying goes, there's more than one way to do it ;-)

python2.6 itertools introduces the izip_longest function and the grouper 
recipe :

def grouper(n, iterable, fillvalue=None):
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return izip_longest(fillvalue=fillvalue, *args)

-- 
 Ned Deily,
 n...@acm.org

--
http://mail.python.org/mailman/listinfo/python-list


Re: slice iterator?

2009-05-08 Thread John O'Hagan
On Sat, 9 May 2009, John O'Hagan wrote:
> On Sat, 9 May 2009, Ross wrote:
> > lists. Let's say I had a list a = [1,2,3,4,5,6,7,8,9,10,11,12] and I
> > wanted to split it into groups of 2 or groups of 3 or 4, etc. Is there
> > a way to do this without explicitly defining new lists? If the above
> > problem were to be split into groups of 3, I've tried something like:
[..]
>
> This seems to work:
> >>> for i in range (len(a) / 3):
>
> ... segment = a[3 * i:3 * (i + 1)]
> ... print segment
[...]

But if len(a) is not divisible by the increment, that misses the last (short) 
slice, so using the step argument to range:

>>> for i in range (0, len(a), 3):
... segment = a[i:i + 3]
... print segment

is better. Or as a list comprehension:

[a[i:i + 3] for i in range (0, len(a), 3)]


HTH,

John
--
http://mail.python.org/mailman/listinfo/python-list


Re: slice iterator?

2009-05-08 Thread John O'Hagan
On Sat, 9 May 2009, Ross wrote:
> lists. Let's say I had a list a = [1,2,3,4,5,6,7,8,9,10,11,12] and I
> wanted to split it into groups of 2 or groups of 3 or 4, etc. Is there
> a way to do this without explicitly defining new lists? If the above
> problem were to be split into groups of 3, I've tried something like:
>
> start = 0
> stop = 3
> for i in range(len(a)):
>     segment = a[start:stop]
>     print segment
>     start += stop
>     stop += stop

This doesn't work because you're looping over every element in your list 
(instead of every slice) and because you're incrementing the increment by 
using "stop" for two different things (endpoint and increment).

This seems to work:

>>> for i in range (len(a) / 3):
... segment = a[3 * i:3 * (i + 1)]
... print segment

Regards,

john
--
http://mail.python.org/mailman/listinfo/python-list


Re: slice iterator?

2009-05-08 Thread Paul Rubin
Ross  writes:
> I have a really long list that I would like segmented into smaller
> lists. Let's say I had a list a = [1,2,3,4,5,6,7,8,9,10,11,12] and I
> wanted to split it into groups of 2 or groups of 3 or 4, etc. Is there
> a way to do this without explicitly defining new lists? 

That question comes up so often it should probably be a standard
library function.

Anyway, here is an iterator, if that's what you want:

   >>> from itertools import islice
   >>> a = range(12)
   >>> xs = iter(lambda x=iter(a): list(islice(x,3)), [])
   >>> print list(xs)
   [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]]

Of course, as the saying goes, there's more than one way to do it ;-)
--
http://mail.python.org/mailman/listinfo/python-list