Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Packing a list of lists with struct.pack()

Reply
Thread Tools

Packing a list of lists with struct.pack()

 
 
Panos Laganakos
Guest
Posts: n/a
 
      04-24-2006
Hello,

I have a list that includes lists of integers, in the form of:
li = [[0, 1, 2], [3, 4, 5], ...]

packed = struct.pack(str(len(li)*3)+'i', li)

The frmt part is right, as I'm multiplying by 3, 'cause each inner list
has 3 elements.

What can I do to get li as a single list of integers?

I tried list comprehension in the form of:
[([j for j in i]) for i in li]

But that doesn't seem to work, any ideas?

 
Reply With Quote
 
 
 
 
Panos Laganakos
Guest
Posts: n/a
 
      04-24-2006
Just came up with this:

litemp = []

[litemp.extend(i) for i in li]

Seems to give me a list with all the inner elements of li, not sure if
struct.pack will accept it now, but I'll give it a try.

 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      04-24-2006
Panos Laganakos wrote:

> I have a list that includes lists of integers, in the form of:
> li = [[0, 1, 2], [3, 4, 5], ...]


> What can I do to get li as a single list of integers?
>
> I tried list comprehension in the form of:
> [([j for j in i]) for i in li]
>
> But that doesn't seem to work, any ideas?


you have it backwards: a nested list expression is like a nested
for loop, but with the innermost expression at the beginning. a
for-loop would look like:

for i in li:
for j in i:
... do something with j ...

so the corresponding comprehension is

[j for i in li for j in i]

which gives you the expected result. when you pass this to pack,
you can use a generator expression instead:

data = struct.pack("%di" % (len(li)*3), *(j for i in li for j in i))

</F>



 
Reply With Quote
 
Panos Laganakos
Guest
Posts: n/a
 
      04-26-2006
Fredrik, thanks alot.

Your preposition worked like a charm, plus I got to learn how to
reverse an inner for loop using list comprehensions

What I don't understand is what are you doing with *(...), this is
supposed to pack its contents as a list?

 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      04-27-2006
Panos Laganakos wrote:

> What I don't understand is what are you doing with *(...), this is
> supposed to pack its contents as a list?


the *arg form treats each value in the arg sequence as a separate argument.
for example,

arg = 1, 2, 3
function(*arg)

is the same thing as

function(1, 2, 3)

when used with a generator expression, *(genexp) simply fetches the arguments
from the generated sequence.

(note that struct.pack expects a format string plus N additional arguments, not
a format string and an N-item sequence)

</F>



 
Reply With Quote
 
Panos Laganakos
Guest
Posts: n/a
 
      04-27-2006
Unfortunately I'm familiar with the generator concept, I'll look into
it though, 'cause it saved me at least once already

Thanks mate.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
common elements between list of lists and lists antar2 Python 2 07-17-2008 09:19 AM
How to have a list of lists (or array of lists) bahoo Python 3 04-03-2007 07:37 PM
list of lists of lists .... yomgui Python 6 07-31-2006 07:28 PM
List of lists of lists of lists... =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==?= Python 5 05-15-2006 11:47 AM
Packing list elements into tuples Nickolay Kolev Python 8 11-10-2004 04:49 PM



Advertisments