Re: function for counting items in a sequence

2007-04-04 Thread Alex Martelli
Steven Bethard <[EMAIL PROTECTED]> wrote: > Alex Martelli wrote: > > If we had a "turn sequence into bag" function somewhere > > (and it might be worth having it for other reasons): > > > > def bagit(seq): > > import collections > > d = collections.defaultdict(int) > > for x in seq: d

function for counting items in a sequence

2007-04-04 Thread Steven Bethard
Alex Martelli wrote: > If we had a "turn sequence into bag" function somewhere > (and it might be worth having it for other reasons): > > def bagit(seq): > import collections > d = collections.defaultdict(int) > for x in seq: d[x] += 1 > return d I use this function all the time --

Re: counting items

2005-01-13 Thread Craig Ringer
On Wed, 2005-01-12 at 20:10 +0100, Bernhard Herzog wrote: > "It's me" <[EMAIL PROTECTED]> writes: > > > May be flatten should be build into the language somehow > > That shouldn't be necessary as it can easily be written in a single list > comprehension: > > a = [[1,2,4],4,5,[2,3]] > flat_a

Re: counting items

2005-01-12 Thread Bengt Richter
On Wed, 12 Jan 2005 18:07:47 GMT, "Andrew Koenig" <[EMAIL PROTECTED]> wrote: >"It's me" <[EMAIL PROTECTED]> wrote in message >news:[EMAIL PROTECTED] > >> What's the best way to count number of items in a list? >> >> For instance, >> >> a=[[1,2,4],4,5,[2,3]] >> >> I want to know how many items are

list and generators (WAS: counting items)

2005-01-12 Thread Steven Bethard
Michael Hartl wrote: That's cool! Of course, walk returns a generator, so using a list comprehension to turn it into a list seems natural, but I didn't realize that list() does the same thing (and neither, apparently, did the original implementor) -- although, with a little reflection, it obviousl

Re: counting items

2005-01-12 Thread Michael Hartl
That's cool! Of course, walk returns a generator, so using a list comprehension to turn it into a list seems natural, but I didn't realize that list() does the same thing (and neither, apparently, did the original implementor) -- although, with a little reflection, it obviously must! Michael --

Re: counting items

2005-01-12 Thread Steven Bethard
Michael Hartl wrote: (Once you can iterate over an arbitrary sequence, the flattened version is just [element for element in walk(sequence)].) Or, better yet: list(walk(sequence)) Steve -- http://mail.python.org/mailman/listinfo/python-list

Re: counting items

2005-01-12 Thread Michael Hartl
There's a great function called "walk" at that iterates over arbitrary data (and is recursion-proof) at http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/118845 It also supplies a recursion-proof way to flatten a list. (Once you can iterate over an arbitrary sequence, the flattened version

Re: counting items

2005-01-12 Thread Leif K-Brooks
Paul McGuire wrote: Considering how often this comes up, might there be a place for some sort of flatten() routine in the std dist, perhaps itertools? A problem I see is that itertools tries to work across all iterable types, but flattening can't always be a generalized operation. For instance, a

Re: counting items

2005-01-12 Thread It's me
Thanks. May be flatten should be build into the language somehow "Paul McGuire" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > "It's me" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > > Okay, I give up. > > > > What's the best way to count number of items in a

Re: counting items

2005-01-12 Thread Bernhard Herzog
"It's me" <[EMAIL PROTECTED]> writes: > May be flatten should be build into the language somehow That shouldn't be necessary as it can easily be written in a single list comprehension: a = [[1,2,4],4,5,[2,3]] flat_a = [x for cur, rest in [[a[:1], a[1:]]] for x in cur if (not isin

Re: counting items

2005-01-12 Thread Steven Bethard
Mark McEahern wrote: It's me wrote: Okay, I give up. What's the best way to count number of items in a list [that may contain lists]? a = [[1,2,4],4,5,[2,3]] def iterall(seq): for item in seq: try: for subitem in iterall(item): yield subitem except Typ

Re: counting items

2005-01-12 Thread It's me
Yes, Mark, I came up with almost the same code (after reading back old answers from this list): def flatten(seq): for x in seq: if hasattr(x, "__iter__"): for subx in flatten(x): yield subx else: yield x def count_item(data): return

Re: counting items

2005-01-12 Thread It's me
Oh, darn. I asked this kind of question before. Somebody posted an answer before: def flatten(seq): for x in seq: if hasattr(x, "__iter__"): for subx in flatten(x): yield subx else: yield x data = [[1,5,2],8,4] val_to_pos = {} for i,

Re: counting items

2005-01-12 Thread Anthony
On Wed, 12 Jan 2005 17:42:50 GMT, It's me <[EMAIL PROTECTED]> wrote: > Okay, I give up. > > What's the best way to count number of items in a list? How long is a piece of string? There are many different ways, which give you different trade offs. > For instance, > > a=[[1,2,4],4,5,[2,3]] > > I

Re: counting items

2005-01-12 Thread Paul McGuire
"It's me" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Okay, I give up. > > What's the best way to count number of items in a list? > > For instance, > > a=[[1,2,4],4,5,[2,3]] > > I want to know how many items are there in a (answer should be 7 - I don't > want it to be 4) > I've

Re: counting items

2005-01-12 Thread Mark McEahern
It's me wrote: Okay, I give up. What's the best way to count number of items in a list [that may contain lists]? a = [[1,2,4],4,5,[2,3]] def iterall(seq): for item in seq: try: for subitem in iterall(item): yield subitem except TypeError: yie

Re: counting items

2005-01-12 Thread Andrew Koenig
"It's me" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > What's the best way to count number of items in a list? > > For instance, > > a=[[1,2,4],4,5,[2,3]] > > I want to know how many items are there in a (answer should be 7 - I don't > want it to be 4) How about this? def t

counting items

2005-01-12 Thread It's me
Okay, I give up. What's the best way to count number of items in a list? For instance, a=[[1,2,4],4,5,[2,3]] I want to know how many items are there in a (answer should be 7 - I don't want it to be 4) I tried: b=len([x for y in a for x in y]) That doesn't work because you would get an iterat