Re: #elements of seq A in seq B

2009-08-21 Thread Raymond Hettinger
On Aug 19, 4:19 pm, Neal Becker ndbeck...@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)

Re: #elements of seq A in seq B

2009-08-21 Thread Raymond Hettinger
On Aug 19, 4:19 pm, Neal Becker ndbeck...@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)

Re: #elements of seq A in seq B

2009-08-21 Thread Peter Otten
Jan Kaliszewski wrote: 20-08-2009 o 13:01:29 Neal Becker ndbeck...@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

Re: #elements of seq A in seq B

2009-08-20 Thread Neal Becker
Jan Kaliszewski wrote: 20-08-2009 o 01:19:24 Neal Becker ndbeck...@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

Re: #elements of seq A in seq B

2009-08-20 Thread Peter Otten
Neal Becker wrote: I meant #occurrences of characters from the set A in string B If a contains few characters: n = sum(b.count(c) for c in a) If a contains many characters: identity = .join(map(chr, range(256))) n = len(b) - len(b.translate(identity, a)) Peter --

Re: #elements of seq A in seq B

2009-08-20 Thread Jan Kaliszewski
a = set(a) n = sum(item in a for item in b) Why set? Does it matter if I say that items in A are already unique? Sets are hash-based, so it's (most probably) far more efficient for sets than for sequences (especially if we say about big/long ones). Regards, *j -- Jan Kaliszewski

#elements of seq A in seq B

2009-08-19 Thread Neal Becker
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). -- http://mail.python.org/mailman/listinfo/python-list

Re: #elements of seq A in seq B

2009-08-19 Thread Jan Kaliszewski
20-08-2009 o 01:19:24 Neal Becker ndbeck...@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

Re: #elements of seq A in seq B

2009-08-19 Thread Simon Forman
On Aug 19, 8:17 pm, Jan Kaliszewski z...@chopin.edu.pl wrote: 20-08-2009 o 01:19:24 Neal Becker ndbeck...@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,

Re: #elements of seq A in seq B

2009-08-19 Thread Tomasz Rola
On Wed, 19 Aug 2009, Neal Becker 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 A and B are rather lengthy, then maybe build a tree from

Re: #elements of seq A in seq B

2009-08-19 Thread John Machin
On Aug 20, 12:12 pm, Simon Forman sajmik...@gmail.com wrote: On Aug 19, 8:17 pm, Jan Kaliszewski z...@chopin.edu.pl wrote: If you mean: to count non overlaping occurences of string A in B -- simply:    B.count(A) You don't want to use count() in a case like this because it iterates

Re: #elements of seq A in seq B

2009-08-19 Thread Simon Forman
On Aug 19, 11:34 pm, John Machin sjmac...@lexicon.net wrote: On Aug 20, 12:12 pm, Simon Forman sajmik...@gmail.com wrote: On Aug 19, 8:17 pm, Jan Kaliszewski z...@chopin.edu.pl wrote: If you mean: to count non overlaping occurences of string A in B -- simply:    B.count(A) You