Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeder2.ecngs.de!ecngs!feeder.ecngs.de!Xl.tags.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!local2.nntp.ams.giganews.com!nntp.brightview.co.uk!news.brightview.co.uk.POSTED!not-for-mail NNTP-Posting-Date: Mon, 22 Apr 2013 11:38:19 -0500 Message-ID: <51756769.20206@nowhere.org> Date: Mon, 22 Apr 2013 17:38:01 +0100 From: Blind Anagram User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20130328 Thunderbird/17.0.5 MIME-Version: 1.0 Newsgroups: comp.lang.python To: python-list@python.org Subject: Re: List Count References: <5175377f$0$29977$c3e8da3$5496439d@news.astraweb.com> <517545F7.5090209@nowhere.org> <51755C38.4000204@nowhere.org> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Lines: 48 X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-2ajJluLhAJAjws4N9te8Mh+kEJqBVHwBjKWcUwVHXWJJBAzQTr84F0eu9EZBShc9Pf+BGm0C8bm2VX/!F0Fwb5RLpddonVv+P5MifSaVOiy3nFKaPemHi6pCRMOsVlMcl48hbC8oe6cIg8KhzTnpAH4= X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 3409 Xref: csiph.com comp.lang.python:44095 On 22/04/2013 17:06, Oscar Benjamin wrote: > On 22 April 2013 16:50, Blind Anagram wrote: >>> >>> It would be very easy to subclass list and add this functionality in >>> cython if you decide that you do need a builtin method. > [snip] >> >> But I was really wondering if there was a simple solution that worked >> without people having to add libraries to their basic Python installations. > > There are simple solutions and some have already been listed. You are > attempting to push your program to the limit of your hardware > capabilities and it's natural that in a high-level language you'll > often want special libraries for that. Hi Oscar Yes, but it is a tribute to Python that I can do this quite fast for huge lists provided that I only count on the full list. And, unless I have completely misunderstood Python internals, it would probably be just as fast on a sub-sequence if I had a list.count(value, limit) function (however, I admit that I could be wrong here since the fact that count on lists does not offer this may mean that it is not as easy to implement as it might seem). > I don't know what your application is but I would say that my first > port of call here would be to consider a different algorithmic > approach. An obvious question would be about the sparsity of this data > structure. How frequent are the values that you are trying to count? > Would it make more sense to store a list of their indices? Actually it is no more than a simple prime sieve implemented as a Python class (and, yes, I realize that there are plenty of these around). > If the problem needs to be solved the way that you are currently doing > it and the available methods are not fast enough then you will need to > consider additional libraries. >> >> As I have never tried building an extension with cython, I am inclined >> to try this as a learning exercise if nothing else. > > I definitely recommend this over writing a C extension directly. Thanks again - I will definitely look at this. Brian