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 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 be

Re: #elements of seq A in seq B

2009-08-20 Thread Raymond Hettinger
On Aug 19, 4:19 pm, 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). Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit

Re: #elements of seq A in seq B

2009-08-20 Thread Raymond Hettinger
On Aug 19, 4:19 pm, 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). Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit

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

Re: #elements of seq A in seq B

2009-08-20 Thread Jan Kaliszewski
20-08-2009 o 13:01:29 Neal Becker 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 <

Re: #elements of seq A in seq B

2009-08-20 Thread Jan Kaliszewski
20-08-2009 o 04:12:14 Simon Forman wrote: If you want to know the count for each element you can use this: from collections import defaultdict def g(a, b): a = set(a) d = defaultdict(int) for item in b: if item in a: d[item] += 1 return d print g(A, B) #

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 -- http://mail.python

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 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 occuren

Re: #elements of seq A in seq B

2009-08-19 Thread Simon Forman
On Aug 19, 11:34 pm, John Machin wrote: > On Aug 20, 12:12 pm, Simon Forman wrote: > > > On Aug 19, 8:17 pm, "Jan Kaliszewski" 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 t

Re: #elements of seq A in seq B

2009-08-19 Thread John Machin
On Aug 20, 12:12 pm, Simon Forman wrote: > On Aug 19, 8:17 pm, "Jan Kaliszewski" 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 > through B len(A) times, i.e.

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 elem

Re: #elements of seq A in seq B

2009-08-19 Thread Simon Forman
On Aug 19, 8:17 pm, "Jan Kaliszewski" wrote: > 20-08-2009 o 01:19:24 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 you

Re: #elements of seq A in seq B

2009-08-19 Thread Jan Kaliszewski
20-08-2009 o 01:19:24 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 you mean: to count occurences of each element of A (separately)

#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