list comprehension question
brueckd at tbye.com
brueckd at tbye.com
Sun Mar 24 19:37:19 EST 2002
On Sun, 24 Mar 2002, Tripp Scott wrote:
> can i generate this list:
>
> [1, 1.1, 2, 2.1, 3, 3.1]
>
> with a list comprehension in a form of this:
>
> [SOMETHING for x in 1,2,3]
>
> and without using side effects like this?
>
> result=[]
> [(result.append(x), result.append(x+1.1)) for x in 1,2,3]
You gotta give us more info on the 1,2,3 part. Does that just represent
a sequence of integers? If so, you can go with something like:
>>> lo, hi = 1, 3
>>> [x/2 + 0.1*(x%2) for x in xrange(lo*2, (hi+1)*2)]
-Dave
More information about the Python-list
mailing list