Reputation: 191
How am I not having the same results from this loop..
for i in range(0,len(irradiance_list_bytes),28):
dardy= struct.unpack("qHHHHfff",irradiance_list_bytes[i:i+28])
When I transform it to a list comprehension like this...
dardy=[struct.unpack("qHHHHfff",irradiance_list_bytes[i:i+28]) for i in range(0,len(irradiance_list_bytes),28)]
The result of the loop is:
dardy
Out[55]:
(631810591,
32,
12,
1,
100,
146.53225708007812,
-4.72298002243042,
-7.121456623077393)
And the result of the list of comprehension is:
dardy
Out[57]:
[(629816865,
32,
12,
1,
100,
143.21139526367188,
-3.786829710006714,
-6.368762016296387),
(630014820,
32,
12,
1,
100,
143.46746826171875,
-3.9606733322143555,
-6.6814117431640625),
(630213227,
32,
12,
1,
100,
143.42613220214844,
-3.992025136947632,
-6.901387691497803),
......]
Upvotes: 0
Views: 84
Reputation: 78546
You don't need to build a list or use a for loop to get the last slice of items.
Use the mod operator to compute the start index of the last slice when the stride value is 28. If the list length is a factor of 28, subtract 28 from the length, otherwise subtract length mod
28 from the length:
l = len(irradiance_list_bytes)
dardy= struct.unpack("qHHHHfff", irradiance_list_bytes[l - (l%28 or 28):])
Upvotes: 1
Reputation: 42678
Just take the last value:
dardy=[struct.unpack("qHHHHfff",irradiance_list_bytes[i:i+28]) for i in range(0,len(irradiance_list_bytes),28)][-1]
Upvotes: 1