#elements of seq A in seq B

Jan Kaliszewski zuo at chopin.edu.pl
Thu Aug 20 19:25:38 EDT 2009


20-08-2009 o 04:12:14 Simon Forman <sajmikins at gmail.com> wrote:

> If you want to know the count for each element you can use this:
>
> from collections import defaultdict
>
> def g(a, b):
>     a = set(a)
>     d = defaultdict(int)
>     for item in b:
>         if item in a:
>             d[item] += 1
>     return d
>
> print g(A, B)
>
> # prints defaultdict(<type 'int'>, {' ': 1, 'e': 1, 'g': 1, 'i': 1,
> 'o': 1, 'n': 2, 's': 1, 'r': 2, 't': 2})

Yeah, your sollution is better (and more interesting :-)). Thanks!

*j

-- 
Jan Kaliszewski (zuo) <zuo at chopin.edu.pl>



More information about the Python-list mailing list