List Count
88888 Dihedral
dihedral88888 at googlemail.com
Mon Apr 22 09:36:29 EDT 2013
Blind Anagram於 2013年4月22日星期一UTC+8下午7時58分20秒寫道:
> 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?
For those problems related to a homogeneous list of numbers
, please check whether the arrays in numpy can fit your needs practically or not.
Sometimes I work on numbers in varied ranges,
then the list and the long integers in Python is really handy.
More information about the Python-list
mailing list