#elements of seq A in seq B

Peter Otten __peter__ at web.de
Fri Aug 21 03:43:51 EDT 2009


Jan Kaliszewski wrote:

> 20-08-2009 o 13:01:29 Neal Becker <ndbecker2 at gmail.com> wrote:
> 
>> I meant #occurrences of characters from the set A in string B
> 
> But:
> 
> 1) separately for each element of A? (see Simon's sollution with
> defaultdict)
> 
> 2) or total number of all occurrences of elements of A? (see below)
> 
> 
> 20-08-2009 o 14:05:12 Peter Otten <__peter__ at web.de> wrote:
> 
>> identity = "".join(map(chr, range(256)))
>> n = len(b) - len(b.translate(identity, a))
> 
> Nice, though I'd prefer Simon's sollution:
> 
>      a = set(a)
>      n = sum(item in a for item in b)

Just to give you an idea why I posted the former:

$ python -m timeit -s"a = set('abc'); b = 'abcdefg'*10**5" 'sum(item in a 
for item in b)'
10 loops, best of 3: 195 msec per loop

$ python -m timeit -s"a = 'abc'; b = 'abcdefg'*10**5; id=''.join(map(chr, 
range(256)))" 'len(b) - len(b.translate(id, a))'
100 loops, best of 3: 4.97 msec per loop

Peter




More information about the Python-list mailing list