Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #101328
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Fast pythonic way to process a huge integer list |
| Date | 2016-01-07 11:21 +0100 |
| Organization | None |
| Message-ID | <mailman.40.1452162085.2305.python-list@python.org> (permalink) |
| References | <7e2b93e4-c224-40c4-8e88-7dcc847edab1@googlegroups.com> |
high5storage@gmail.com wrote:
> I have a list of 163.840 integers. What is a fast & pythonic way to
> process this list in 1,280 chunks of 128 integers?
What kind of processing do you have in mind?
If it is about numbercrunching use a numpy.array. This can also easily
change its shape:
>>> import numpy
>>> a = numpy.array(range(12))
>>> a
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
>>> a.shape = (3, 4)
>>> a
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
If it's really only(!) under a million integers slicing is also good:
items = [1, 2, ...]
CHUNKSIZE = 128
for i in range(0, len(items), CHUNKSIZE):
process(items[start:start + CHUNKSIZE])
If the "list" is really huge (your system starts swapping memory) you can go
completely lazy:
from itertools import chain, islice
def chunked(items, chunksize):
items = iter(items)
for first in items:
chunk = chain((first,), islice(items, chunksize-1))
yield chunk
for dummy in chunk: # consume items that may have been skipped
# by your processing
pass
def produce_items(file):
for line in file:
yield int(line)
CHUNKSIZE = 128 # this could also be "huge"
# without affecting memory footprint
with open("somefile") as file:
for chunk in chunked(produce_items(file), CHUNKSIZE):
process(chunk)
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Fast pythonic way to process a huge integer list high5storage@gmail.com - 2016-01-06 18:36 -0800 Re: Fast pythonic way to process a huge integer list Terry Reedy <tjreedy@udel.edu> - 2016-01-06 22:10 -0500 Re: Fast pythonic way to process a huge integer list Tim Chase <python.list@tim.thechases.com> - 2016-01-06 21:21 -0600 Re: Fast pythonic way to process a huge integer list Cameron Simpson <cs@zip.com.au> - 2016-01-07 14:31 +1100 Re: Fast pythonic way to process a huge integer list Steven D'Aprano <steve@pearwood.info> - 2016-01-07 20:25 +1100 Re: Fast pythonic way to process a huge integer list Peter Otten <__peter__@web.de> - 2016-01-07 11:21 +0100 Re: Fast pythonic way to process a huge integer list KP <kai.peters@gmail.com> - 2016-01-07 16:33 -0800
csiph-web