vector addition

Thomas Jollans tjol at jollybox.de
Mon Jun 7 16:16:39 EDT 2010


On 06/07/2010 07:45 PM, Dan Stromberg wrote:
>
> Call me strange, but I regard this as a good place to use a functional
> style - IE, to use reduce, and furthermore I regard this as a good
> example of why reduce is useful for more than just summing numbers:
>
> #!/disc/gx/sdfw/dans/python26/bin/python
this line is shouting to be replaced with
#!/usr/bin/env python
;-)
>
> import collections
>
> def count_first_letters(dictionary, string_):
>    dictionary[string_[0]] += 1
>    return dictionary
>
> print reduce(
>    count_first_letters,
>    ['abc', 'def', 'air', 'ghi'],
>    collections.defaultdict(int),
>    )
>
>
> On Sat, Jun 5, 2010 at 7:01 PM, MRAB <python at mrabarnett.plus.com
> <mailto:python at mrabarnett.plus.com>> wrote:
>
>     GZ wrote:
>
>         Hi,
>
>         I am looking for a fast internal vector representation so that
>         (a1,b2,c1)+(a2,b2,c2)=(a1+a2,b1+b2,c1+c2).
>
>         So I have a list
>
>         l = ['a'a,'bb','ca','de'...]
>
>         I want to count all items that start with an 'a', 'b', and 'c'.
>
>         What I can do is:
>
>         count_a = sum(int(x[1]=='a') for x in l)
>         count_b = sum(int(x[1]=='b') for x in l)
>         count_c = sum(int(x[1]=='c') for x in l)
>
>         But this loops through the list three times, which can be slow.
>
>         I'd like to have something like this:
>         count_a, count_b, count_c =
>         sum( (int(x[1]=='a',int(x[1]=='b',int(x[1]=='c')   for x in l)
>
>         I hesitate to use numpy array, because that will literally
>         create and
>         destroy a ton of the arrays, and is likely to be slow.
>
>     If you want to do vector addition then numpy is the way to go.
>     However,
>     first you could try:
>
>        from collections import defaultdict
>        counts = defaultdict(int)
>        for x in l:
>            counts[x[0]] += 1
>
>     (Note that in Python indexes are zero-based.)
>
>     -- 
>     http://mail.python.org/mailman/listinfo/python-list
>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100607/5c21a53b/attachment-0001.html>


More information about the Python-list mailing list