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
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
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
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
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 <
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)
#
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
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
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
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.
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
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
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)
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
14 matches
Mail list logo