#elements of seq A in seq B
Neal Becker
ndbecker2 at gmail.com
Thu Aug 20 07:01:29 EDT 2009
Jan Kaliszewski wrote:
> 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
>
I meant #occurrences of characters from the set A in string B
More information about the Python-list
mailing list