#elements of seq A in seq B

Jan Kaliszewski zuo at chopin.edu.pl
Wed Aug 19 20:17:47 EDT 2009


20-08-2009 o 01:19:24 Neal Becker <ndbecker2 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).

If you mean: to count occurences of each element of A (separately) in B...

Hm, maybe something like this:

   # result as a dict {<element of A>: <how many occurences in B>, ...}
   dict((element, B.count(element)) for element in A)


If you mean: to count non overlaping occurences of string A in B
-- simply:

   B.count(A)


Regards,
*j

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



More information about the Python-list mailing list