#elements of seq A in seq B

Raymond Hettinger python at rcn.com
Fri Aug 21 08:44:29 CEST 2009

On Aug 19, 4:19 pm, Neal Becker <ndbeck... at gmail.com> wrote:
> What would be a time efficient way to count the number of occurrences of
> elements of sequence A in sequence B?  (in this particular case, these
> sequences are strings, if that matters).

Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit
(Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import collections
>>> A = 'abc'
>>> B = 'abracadabra'
>>> collections.Counter(filter(set(A).__contains__, B))
Counter({'a': 5, 'b': 2, 'c': 1})


More information about the Python-list mailing list