Re: tricky nested list unpacking problem

2008-12-16 Thread Steve Holden
Chris Rebert wrote: > On Mon, Dec 15, 2008 at 11:06 AM, Reckoner wrote: >> Hi, >> >> I have lists of the following type: >> >> [1,2,3,[5,6]] >> >> and I want to produce the following strings from this as >> >> '0-1-2-3-5' >> '0-1-2-3-6' >> >> That was easy enough. The problem is that these can be

Re: tricky nested list unpacking problem

2008-12-16 Thread Arnaud Delobelle
bearophileh...@lycos.com writes: > I was waiting to answer because so far I have found a bad-looking > solution only. Seeing there's only your solution, I show mine too. It > seems similar to your one. I think that the solution below is a bit clearer, although I think it is more resource intensiv

Re: tricky nested list unpacking problem

2008-12-15 Thread Reckoner
On Dec 15, 1:28 pm, Arnaud Delobelle wrote: > Reckoner writes: > > Hi, > > > I have lists of the following type: > > > [1,2,3,[5,6]] > > > and I want to produce the following strings from this as > > > '0-1-2-3-5' > > '0-1-2-3-6' > > > That was easy enough. The problem is that these can be nested.

Re: tricky nested list unpacking problem

2008-12-15 Thread Chris Rebert
On Mon, Dec 15, 2008 at 12:24 PM, Kirk Strauser wrote: > At 2008-12-15T20:03:14Z, "Chris Rebert" writes: > >> You just need a recursive list-flattening function. There are many >> recipes for these. Here's mine: > > flattened = flatten([1,2,3,[5,6,[10, 11]],7,[9,[1, 2, 3, 4, 5 ]]]) > flat

Re: tricky nested list unpacking problem

2008-12-15 Thread bearophileHUGS
Arnaud Delobelle: > Here is a not thought out solution: >... I was waiting to answer because so far I have found a bad-looking solution only. Seeing there's only your solution, I show mine too. It seems similar to your one. def xflatten(seq): if isinstance(seq, list): stack = [iter(se

Re: tricky nested list unpacking problem

2008-12-15 Thread Scott David Daniels
Kirk Strauser wrote: At 2008-12-15T19:06:16Z, Reckoner writes: The problem is that I don't know ahead of time how many lists there are or how deep they go. In other words, you could have: Recursion is your friend. Write a function to unpack one "sublist" and call itself again with the new l

Re: tricky nested list unpacking problem

2008-12-15 Thread Arnaud Delobelle
Reckoner writes: > Hi, > > I have lists of the following type: > > [1,2,3,[5,6]] > > and I want to produce the following strings from this as > > '0-1-2-3-5' > '0-1-2-3-6' > > That was easy enough. The problem is that these can be nested. For > example: > > [1,2,3,[5,6],[7,8,9]] > > which should

Re: tricky nested list unpacking problem

2008-12-15 Thread Kirk Strauser
At 2008-12-15T20:03:14Z, "Chris Rebert" writes: > You just need a recursive list-flattening function. There are many > recipes for these. Here's mine: flattened = flatten([1,2,3,[5,6,[10, 11]],7,[9,[1, 2, 3, 4, 5 ]]]) flattened > [1, 2, 3, 5, 6, 10, 11, 7, 9, 1, 2, 3, 4, 5] '-'.jo

Re: tricky nested list unpacking problem

2008-12-15 Thread Kirk Strauser
At 2008-12-15T19:06:16Z, Reckoner writes: > The problem is that I don't know ahead of time how many lists there are or > how deep they go. In other words, you could have: Recursion is your friend. Write a function to unpack one "sublist" and call itself again with the new list. For instance, s

Re: tricky nested list unpacking problem

2008-12-15 Thread Chris Rebert
On Mon, Dec 15, 2008 at 11:06 AM, Reckoner wrote: > Hi, > > I have lists of the following type: > > [1,2,3,[5,6]] > > and I want to produce the following strings from this as > > '0-1-2-3-5' > '0-1-2-3-6' > > That was easy enough. The problem is that these can be nested. For > example: > > [1,2,3,

tricky nested list unpacking problem

2008-12-15 Thread Reckoner
Hi, I have lists of the following type: [1,2,3,[5,6]] and I want to produce the following strings from this as '0-1-2-3-5' '0-1-2-3-6' That was easy enough. The problem is that these can be nested. For example: [1,2,3,[5,6],[7,8,9]] which should produce '0-1-2-3-5-7' '0-1-2-3-5-8' '0-1-2-3-

Re: Unpacking problem

2007-03-01 Thread timm . gloger
Ok, that solves my confusion. Thanks, Marc. -- http://mail.python.org/mailman/listinfo/python-list

Re: Unpacking problem

2007-03-01 Thread timm . gloger
The problem is, that len('\x90\x06\x00') is not equivalent to calcsize('Bh'): >>> calcsize('Bh') 4 >>> len('\x90\x06\x00') 3 Actually calculating the size for 'hB' results in: >>> calcsize('hB') 3 So far I have not figured out, why there is an additional byte, but it does not effect the result in

Re: Unpacking problem

2007-03-01 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, Chris Garland wrote: > But an unsigned char & a short give me this unpack('Bh','\x90\x06\x00') > Traceback (most recent call last): > File "", line 1, in ? > struct.error: unpack str size does not match format Let's pack this: In [90]: pack('Bh', 0x90, 0x6) Out[90]

Unpacking problem

2007-03-01 Thread Chris Garland
What's wrong here? >>> from struct import unpack I can unpack an unsigned char >>> unpack('B','\x90') (144,) I can unpack a short >>> unpack('h','\x06\x00') (6,) But an unsigned char & a short give me this >>> unpack('Bh','\x90\x06\x00') Traceback (most recent call last): File "", line 1, in