Packing a list of lists with struct.pack()

Discussion in 'Python' started by Panos Laganakos, Apr 24, 2006.

  1. 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?
    Panos Laganakos, Apr 24, 2006
    #1
    1. Advertising

  2. 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.
    Panos Laganakos, Apr 24, 2006
    #2
    1. Advertising

  3. 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>
    Fredrik Lundh, Apr 24, 2006
    #3
  4. 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?
    Panos Laganakos, Apr 26, 2006
    #4
  5. 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>
    Fredrik Lundh, Apr 27, 2006
    #5
  6. Unfortunately I'm familiar with the generator concept, I'll look into
    it though, 'cause it saved me at least once already :)

    Thanks mate.
    Panos Laganakos, Apr 27, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim Jones
    Replies:
    0
    Views:
    376
    Tim Jones
    Jan 31, 2004
  2. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    401
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  3. Jonathan Fine
    Replies:
    9
    Views:
    361
    Jonathan Fine
    Jun 23, 2007
  4. Alexander Farber

    pack 'C3U*' not same as pack 'C3(xC)*'

    Alexander Farber, Jun 23, 2005, in forum: Perl Misc
    Replies:
    2
    Views:
    134
    Ilmari Karonen
    Jun 23, 2005
  5. Replies:
    13
    Views:
    352
    glen herrmannsfeldt
    May 5, 2013
Loading...

Share This Page