bhaaluu wrote:
> Can you explain how this works? How would this be written in
> a "conventional" way?
I'm not sure if this is addressed to me but I'll reply anyway. :)
foo = [1,2,3,4,5,6]
[(foo[i],foo[i+1]) for i in range(0,len(foo),2)]
range(0,len(foo)) would create a list of consecut
Can you explain how this works? How would this be written in
a "conventional" way?
>>> foo = [1,2,3,4,5,6]
>>> [(foo[i],foo[i+1]) for i in range(0,len(foo),2)]
[(1, 2), (3, 4), (5, 6)]
>>> foo = [1,2,3,4,5,6,7,8,9]
>>> [(foo[i],foo[i+1]) for i in range(0,len(foo)-4,2)]
[(1, 2), (3, 4), (5, 6)]
Al
Noufal Ibrahim wrote:
> This seems to work although I'd like comments from the more experienced
> Pythonistas out there.
>
> >>> foo
> [1, 2, 3, 4, 5, 6, 7, 8, 9]
> >>> [(foo[i],foo[i+1]) for i in range(0,len(foo)-2,2)]
> [(1, 2), (3, 4), (5, 6), (7, 8)]
This problem is a perennial favorite o
Fangwen Lu wrote:
> Dear all-
>
> If I have an array array([1, 2, 3, 4, 5, 6]), and I want to get
> ((1,2),(3,4),(5,6)). What should I do?
This seems to work although I'd like comments from the more experienced
Pythonistas out there.
>>> foo
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> [(foo[i],foo[i+1]
Fangwen Lu wrote:
> Dear all-
>
> If I have an array array([1, 2, 3, 4, 5, 6]), and I want to get
> ((1,2),(3,4),(5,6)). What should I do?
First, you read some documentation and tutorials on the language, then
you iterate over the list and build tuples out of the elements.
>
> Thank you!
>
Fangwen Lu wrote:
> Dear all-
>
> If I have an array array([1, 2, 3, 4, 5, 6]), and I want to get
> ((1,2),(3,4),(5,6)). What should I do?
>
tuple([j for (i,j) in enumerate(zip(x, x[1:])) if i % 2 == 0])
___
Tutor maillist - Tutor@python.org
http:
Dear all-
If I have an array array([1, 2, 3, 4, 5, 6]), and I want to get
((1,2),(3,4),(5,6)). What should I do?
Thank you!
Fangwen
___
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor