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
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 --
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
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
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
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
--
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
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
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
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
"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
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
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
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,
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
"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
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
"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
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
19 matches
Mail list logo