tags:

views:

114

answers:

6

I have a list of 16 elements [a00,a01,a02,...,a15] and would like to compute a list [b0,b1,b2,b3,b4,b5,b6,b7] where

b0 = a00*256+a01
b1 = a02*256+a03
b2 = a04*256+a05
(etc.)

what's the easiest way of doing this? (I'm a beginner in python)

+4  A: 

b_list = [a_list[2*i] * 256 + a_list[2*i+1] for i in range(8)]

Note that this only works if a has 16 elements.

A more general solution would be:

b_list = [a_list[2*i] * 256 + a_list[2*i+1] for i in range(len(a_list) / 2)]

as long as a_list has an even number of elements.

danben
oh. i get it. don't you mean [a_list[2*i] * 256 + a_list[2*i+1] for i in range(8)]
Jason S
for i in range(len(a_list) / 2) works for every even combination ;)
D4V360
@Jason S - yes, that is what I meant :) Fixed.
danben
ok, thanks! ---
Jason S
+1  A: 

The first thing that comes to mind:

>>> a=range(16)
>>> b=[a[i]*256+a[i+1] for i in range(0,len(a),+2)]
[1, 515, 1029, 1543, 2057, 2571, 3085, 3599]
MAK
Version wasn't specified in OP's question but, xrange would not work on Python 3.
AJ
xrange is specific to particular versions? hmmm....
Jason S
Yes, `range` in python 3 will work as `xrange` in Python 2. The "old" `range`, creating a complete list disappears in Python 3, if you want to create a list you'll need to do : `list(range)`
Khelben
@AJ: Edited the answer to remove xrange.
MAK
+2  A: 

You can make a comprehension list...

a = [a00, a01,.... ]
#Readed, take a list with 0,2,4... len(a) [15, will stop at 14]
# then make a[i]*256 + a[i+1], so
# a[0]*256+a[1], a[2]*256+a[3], ... a[14]*256+a[15]
b = [ a[i]*256+a[i+1] for i in range(0,len(a),2) ]
Khelben
Seems that everyone is fast... and have the same ideas :-D
Khelben
Just curious, why does this show up as oldest when the post times suggest otherwise?
danben
@danben: your answer was edited fifteen seconds after this answer was posted.
Adam Bernier
@Adam Bernier: Ah, didnt know that edits counted. Although, at the time I posted that comment, MAK's answer hadn't been edited and was also showing the same behavior
danben
@danben: when you first edited your answer the answered time was changed to the edited time. On meta you can probably find out more about how edits are "merged".
Adam Bernier
A: 

What the other two answers suggest is called a "List Comprehension", basically it is the same as doing this:

a = range(16)
b = []
for i in range(0, len(a),2):
   b.append(a[i]*256+a[i+1])

It just does it in one line.

sberry2A
It's also more efficient than making a `for` loop ;-)
Khelben
+2  A: 
lst = [a01, a02, ... ]
result = [256 * a + b for a, b in zip(lst[::2], lst[1::2])]

This avoids the unpythonic use of range.

recursive
Can you explain :: in this context?
danben
Those are slice syntax. It goes start:end:step. Negative indices count from the end of the list. Any omitted element matches 0:-1:1. So, ::2 will return every other element of the list.
recursive
+4  A: 

Not to get too golfy, but if you'd like to avoid subscript hell, you can use this handy zip idiom:

a = range(16)
b = [x*256+y for x,y in zip(*[iter(a)]*2)]
Triptych
how does this work? (confused) I looked at http://docs.python.org/library/functions.html#zip and understand the basics but that * and iter stuff is weird.
Jason S
It creates a list of two iterators over a. The asterisk means the elements of the following list are to be used as parameters for the function call. So two identical iterators are used as arguments to zip. So each iterator just alternates taking elements from the list.
recursive
@Jason yes it's a tricky-looking idiom, but I find it comes in handy a lot. recursive is mostly right in his explanation, but I have to recommend you piece through the code to really understand it, I was similarly confounded upon first seeing it, and found it quite rewarding to pore through.
Triptych
I agree that it's worth it to figure out. See my take on this: http://telliott99.blogspot.com/2010/01/chunks-of-sequence-in-python.html Also, see this question about why one should use itertools to do this: http://stackoverflow.com/questions/2095637
telliott99