I was wondering if there were a well known pattern/idiom for breaking a
sequence into a list of lists, each n elements long, e.g.
[0,1,2,3,4,5,6,7,8,9,10,11] -> [[0,1,2,3],[4,5,6,7],[8,9,10,11]]
This comes up reasonably often in my work, and every time I re-think
about it, and come up with
[ lis[n:n+4] for n in range( 0, len( lis ), 4 ) ]
which seems very kludgy to me, since it uses a range and len, 2 mentions
of the list identifier and 2 literal 4's (which is the size I want to
break into this time).
Is there a better way? 7 1179
Chris Connett wrote: I was wondering if there were a well known pattern/idiom for breaking a sequence into a list of lists, each n elements long, e.g.
[0,1,2,3,4,5,6,7,8,9,10,11] -> [[0,1,2,3],[4,5,6,7],[8,9,10,11]]
This comes up reasonably often in my work, and every time I re-think about it, and come up with [ lis[n:n+4] for n in range( 0, len( lis ), 4 ) ] which seems very kludgy to me, since it uses a range and len, 2 mentions of the list identifier and 2 literal 4's (which is the size I want to break into this time).
Is there a better way?
Found from a Peter Otten post via Google Groups (searching for
"islice list split"): def chunks(s, size):
.... for i in range(0, len(s), size):
.... yield s[i:i+size] s
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] list(chunks(s, 4))
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]] list(chunks([], 4))
[] list(chunks(s[:10], 4))
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9]]
Of course, this uses range and len as well, and two mentions
of the identifier. A function would have let you avoid the
duplicated literal 4, as this does also.
Now, define "better". ;-) Some tasks have an inherent
complexity that can't be shrunk below a certain size...
-Peter
"Chris Connett" <ch*********@yahoo.com> wrote in message
news:41********@buckaroo.cs.rit.edu...
" [ lis[n:n+4] for n in range( 0, len( lis ), 4 ) ] which seems very kludgy to me, since it uses a range and len, "
range and len are kludgy only to non-python programers.
Tom
Chris Connett <ch*********@yahoo.com> wrote in message news:<41********@buckaroo.cs.rit.edu>... I was wondering if there were a well known pattern/idiom for breaking a sequence into a list of lists, each n elements long, e.g.
[0,1,2,3,4,5,6,7,8,9,10,11] -> [[0,1,2,3],[4,5,6,7],[8,9,10,11]]
This comes up reasonably often in my work, and every time I re-think about it, and come up with [ lis[n:n+4] for n in range( 0, len( lis ), 4 ) ] which seems very kludgy to me, since it uses a range and len, 2 mentions of the list identifier and 2 literal 4's (which is the size I want to break into this time).
Is there a better way?
From a post of mine of some time ago ... import itertools def chop(it, n):
.... tup = (iter(it),)*n
.... return itertools.izip(*tup)
.... list(chop([1,2,3,4,5,6],3)) [(1, 2, 3), (4, 5, 6)] list(chop([1,2,3,4,5,6],2)) [(1, 2), (3, 4), (5, 6)] list(chop([1,2,3,4,5,6],1))
[(1,), (2,), (3,), (4,), (5,), (6,)]
Michele Simionato
Am Dienstag, 10. August 2004 06:39 schrieb Michele Simionato: import itertools def chop(it, n): ... tup = (iter(it),)*n ... return itertools.izip(*tup) ... list(chop([1,2,3,4,5,6],3)) [(1, 2, 3), (4, 5, 6)] list(chop([1,2,3,4,5,6],2)) [(1, 2), (3, 4), (5, 6)] list(chop([1,2,3,4,5,6],1)) [(1,), (2,), (3,), (4,), (5,), (6,)]
Problem being:
list(chop([1,2,3,4,5,6],4))
[(1, 2, 3, 4)]
If you actually want to get back everything from iterator, better do something
like:
def ichop(it,n,rtype=list):
it = iter(it)
empty = False
while not empty:
retv = []
while not empty and len(retv) < n:
try:
retv.append(it.next())
except StopIteration:
empty = True
if retv:
yield rtype(retv)
list(ichop([1,2,3,4,5,6],3))
[[1, 2, 3], [4, 5, 6]] list(ichop([1,2,3,4,5,6],2))
[[1, 2], [3, 4], [5, 6]] list(ichop([1,2,3,4,5,6],1))
[[1], [2], [3], [4], [5], [6]] list(ichop([1,2,3,4,5,6],4,tuple))
[(1, 2, 3, 4), (5, 6)] list(ichop([1,2,3,4,5,6],4))
[[1, 2, 3, 4], [5, 6]]
Heiko.
Heiko Wundram <he*****@ceosg.de> wrote in
news:ma**************************************@pyth on.org: Problem being:
list(chop([1,2,3,4,5,6],4)) [(1, 2, 3, 4)]
If you actually want to get back everything from iterator, better do something like:
def ichop(it,n,rtype=list): it = iter(it) empty = False while not empty: retv = [] while not empty and len(retv) < n: try: retv.append(it.next()) except StopIteration: empty = True if retv: yield rtype(retv)
It depends what you actually want to get if the list isn't a multiple of n.
The uneven length tuple at the end seems a bit bad, you are probably better
off with something like this: def chop(it, n):
tup = (itertools.chain(iter(it), (None,)*(n-1)),)*n
return itertools.izip(*tup)
list(chop([1,2,3,4,5,6],4))
[(1, 2, 3, 4), (5, 6, None, None)]
although this still loses your data if n < 1.
Michele Simionato wrote: Chris Connett <ch*********@yahoo.com> wrote in message news:<41********@buckaroo.cs.rit.edu>...
I was wondering if there were a well known pattern/idiom for breaking a sequence into a list of lists, each n elements long, e.g.
[0,1,2,3,4,5,6,7,8,9,10,11] -> [[0,1,2,3],[4,5,6,7],[8,9,10,11]]
This comes up reasonably often in my work, and every time I re-think about it, and come up with [ lis[n:n+4] for n in range( 0, len( lis ), 4 ) ] which seems very kludgy to me, since it uses a range and len, 2 mentions of the list identifier and 2 literal 4's (which is the size I want to break into this time).
Is there a better way?
From a post of mine of some time ago ...
import itertools def chop(it, n): ... tup = (iter(it),)*n ... return itertools.izip(*tup) ... list(chop([1,2,3,4,5,6],3)) [(1, 2, 3), (4, 5, 6)] list(chop([1,2,3,4,5,6],2)) [(1, 2), (3, 4), (5, 6)] list(chop([1,2,3,4,5,6],1))
[(1,), (2,), (3,), (4,), (5,), (6,)]
Michele Simionato
That's slick! :) Though, objectively, it might be less maintainable,
since if I didn't know what it was doing, it'd take me a minute to
figure it out. I'll definitely keep that idea in my toolbox though! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Debajit Adhikary |
last post by:
I'm still pretty new to design patterns... I was wondering, is there
any difference between the Bridge Pattern and Herb Sutter's Pimpl
Idiom?
Both delegate responsibility to an implementation...
|
by: Chris F Clark |
last post by:
1) I would really like to ask this on comp.lang.c#, but no such group
exists. However, perhaps someone in one of the above two groups
will know the answer and help me out.
In my product, I use a...
|
by: Darin |
last post by:
Hi,
I have a winforms app that will need to save certain runtime values that
will need to be accessed by different classes throughout the life of the
app. Is the best approach to create a...
|
by: lisa.engblom |
last post by:
I have two semi related questions...
First, I am trying to output a list of strings to a csv file using the
csv module. The output file separates each letter of the string with a
comma and then...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |