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? 5 14227
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 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, 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 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>
Unfortunately I'm familiar with the generator concept, I'll look into
it though, 'cause it saved me at least once already :)
Thanks mate. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Volkan YAZICI |
last post by:
hi all,
i've been working on a communication server to talk with an io controller
device. i've written a quick and dirty php script a while ago and now
i need to port it to python (for a real...
|
by: gbb0330 |
last post by:
Hi all
I need some advice.
the daily packing number will be used to match a box with its label.
the guys in the warehouse will get a list of items to pack. they will
find the item - put it...
|
by: Kent |
last post by:
Hi!
I want to store data (of enemys in a game) as a linked list, each node will
look something like the following:
struct node
{
double x,y; // x and y position coordinates
struct enemy...
|
by: Jonathan Bartlett |
last post by:
Just finished a new IBM DeveloperWorks article on linked lists, and
thought you all might be interested. It's not an introduction -- it
instead covers some of the more interesting aspects of...
|
by: Shwetabh |
last post by:
Hi,
This is a question asked to me in an interview.
I haven't been able to figure out an answer for
it. Please try to see what can be done about the
following problem.
/* I have been given two...
|
by: Edward Diener |
last post by:
Is the packing alignment of __nogc classes stored as part of the assembly ?
I think it must as the compiler, when referencing the assembly, could not
know how the original data is packed otherwise....
|
by: Gajendra |
last post by:
How does the byte packing and the byte alignment work in VC++ compiler? What is the effect of #pragma pack(n) on the alignment and byte packing
for example while using the structur
struc
double...
|
by: QbProg |
last post by:
Hello,
what I'm saying here is about VS 2005 WITHOUT service pack.
I have two DLL projects, with EXACTLY the same project preferences and
settings. Both have alignment set to "default", it...
|
by: pereges |
last post by:
Hi, I am wondering which of the two data structures (link list or
array) would be better in my situation. I have to create a list of
rays for my ray tracing program.
the data structure of ray...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |