Path: csiph.com!usenet.pasdenom.info!gegeweb.org!newsfeed.kamp.net!newsfeed.kamp.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.016 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'ideally': 0.04; 'list?': 0.07; 'broke': 0.09; 'python': 0.11; 'assume': 0.14; 'creates': 0.14; '(say': 0.16; 'be:': 0.16; 'boolean': 0.16; 'bytearray': 0.16; 'iterators': 0.16; 'itertools': 0.16; 'notation,': 0.16; 'sub-class': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'import': 0.22; 'header:User-Agent:1': 0.23; 'extension': 0.26; 'possibly': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'fastest': 0.30; 'sets': 0.30; "i'm": 0.30; 'lines': 0.31; 'copying': 0.34; 'could': 0.34; 'problem': 0.35; 'advice': 0.35; 'something': 0.35; 'hundreds': 0.35; 'but': 0.35; 'there': 0.35; 'grateful': 0.36; 'subject:List': 0.36; 'possible': 0.36; 'so,': 0.37; 'two': 0.37; 'list': 0.37; 'list.': 0.37; 'problems': 0.38; 'lists.': 0.38; 'somebody': 0.38; 'to:addr :python-list': 0.38; 'aspects': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; "you're": 0.61; 'offer': 0.62; 'provide': 0.64; 'more': 0.64; 'believe': 0.68; 'received:74.208': 0.68; 'default': 0.69; 'costly': 0.84; 'received:74.208.4.194': 0.84 Date: Mon, 22 Apr 2013 08:51:49 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130308 Thunderbird/17.0.4 MIME-Version: 1.0 To: python-list@python.org Subject: Re: List Count References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:rUcjlLBwNpkpMas7UwZkUYs4SAUrWwzKjFpTvxh9UQK QTRgEizdR9b1fSI5obrSWmnk24GKsnDNWWxPCqQauQl2Z46bf1 oXfQvv2+147qa48AUtRSdjBr7+wG5dPHj6MvGsV6HnH+eg3b1v e+N1pCyATGtvPr//XCg4FhyxtZWkPLAGUkg0frfRkwgcFDkzwS 1qN5jXpZj965tPh0pnfT99TfIC/H1vvaqD1dst/SCb2eWY33wV k8LO1cyGJrELwnxg5oBTj9M4njMDJk626Rk/FpWs+iwPzS66br Vl9e4BpzwwguM1+LLOxnFbiW7GDvY6sNq/NIAFxFDN6PmWGGQ= = X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1366635127 news.xs4all.nl 2207 [2001:888:2000:d::a6]:39202 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:44057 On 04/22/2013 07:58 AM, Blind Anagram wrote: > I would be grateful for any advice people can offer on the fastest way > to count items in a sub-sequence of a large list. > > I have a list of boolean values that can contain many hundreds of > millions of elements for which I want to count the number of True values > in a sub-sequence, one from the start up to some value (say hi). > > I am currently using: > > sieve[:hi].count(True) > > but I believe this may be costly because it copies a possibly large part > of the sieve. > > Ideally I would like to be able to use: > > sieve.count(True, hi) > > where 'hi' sets the end of the count but this function is, sadly, not > available for lists. > > The use of a bytearray with a memoryview object instead of a list solves > this particular problem but it is not a solution for me as it creates > more problems than it solves in other aspects of the program. > > Can I assume that one possible solution would be to sub-class list and > create a C based extension to provide list.count(value, limit)? > > Are there any other solutions that will avoid copying a large part of > the list? > Instead of using the default slice notation, why not use itertools.islice() ? Something like (untested): import itertools it = itertools.islice(sieve, 0, hi) sum(itertools.imap(bool, it)) I only broke it into two lines for clarity. It could also be: sum(itertools.imap(bool, itertools.islice(sieve, 0, hi))) If you're using Python 3.x, say so, and I'm sure somebody can simplify these, since in Python 3, many functions already produce iterators instead of lists. -- DaveA