Working with lists

Hi all, I have a list e.g.
bounds [(1950.0, 2100.0), (1800.0, 1850.0), (1600.0, 1630.0), (1400.0, 1420.0), (1200.0, 1210.0), (990, 1018.0), (10, 12), (12.0, 14.0), (14.0, 16.0), (16.0, 18.0), (18.0, 20)]
How can I extract the first value of each pair given in parenthesis i.e. 1950,1800,1600,1400,... ? Nils

On 8/9/07, Nils Wagner <nwagner@iam.uni-stuttgart.de> wrote:
[(1950.0, 2100.0), (1800.0, 1850.0), (1600.0, 1630.0), (1400.0, 1420.0), (1200.0, 1210.0), (990, 1018.0), (10, 12), (12.0, 14.0), (14.0, 16.0), (16.0, 18.0), (18.0, 20)]
How can I extract the first value of each pair given in parenthesis i.e. 1950,1800,1600,1400,... ?
Here's one way: [z[0] for z in bounds]

FWIW, The list comprehension is faster than using map() In [7]: %timeit map(lambda x:x[0],bounds) 10000 loops, best of 3: 49.6 -¦s per loop In [8]: %timeit [x[0] for x in bounds] 10000 loops, best of 3: 20.8 -¦s per loop Gary R. Keith Goodman wrote:
On 8/9/07, Nils Wagner <nwagner@iam.uni-stuttgart.de> wrote:
[(1950.0, 2100.0), (1800.0, 1850.0), (1600.0, 1630.0), (1400.0, 1420.0), (1200.0, 1210.0), (990, 1018.0), (10, 12), (12.0, 14.0), (14.0, 16.0), (16.0, 18.0), (18.0, 20)]
How can I extract the first value of each pair given in parenthesis i.e. 1950,1800,1600,1400,... ?
Here's one way:
[z[0] for z in bounds]

On 8/9/07, Gary Ruben <gruben@bigpond.net.au> wrote:
FWIW, The list comprehension is faster than using map()
In [7]: %timeit map(lambda x:x[0],bounds) 10000 loops, best of 3: 49.6 -¦s per loop
In [8]: %timeit [x[0] for x in bounds] 10000 loops, best of 3: 20.8 -¦s per loop
zip is even faster on my computer:
timeit map(lambda x:x[0], bounds) 100000 loops, best of 3: 5.48 µs per loop timeit [x[0] for x in bounds] 100000 loops, best of 3: 2.69 µs per loop timeit a, b = zip(*bounds) 100000 loops, best of 3: 2.57 µs per loop

"Keith Goodman" <kwgoodman@gmail.com> writes:
On 8/9/07, Gary Ruben <gruben@bigpond.net.au> wrote:
FWIW, The list comprehension is faster than using map()
In [7]: %timeit map(lambda x:x[0],bounds) 10000 loops, best of 3: 49.6 -¦s per loop
In [8]: %timeit [x[0] for x in bounds] 10000 loops, best of 3: 20.8 -¦s per loop
zip is even faster on my computer:
timeit map(lambda x:x[0], bounds) 100000 loops, best of 3: 5.48 µs per loop timeit [x[0] for x in bounds] 100000 loops, best of 3: 2.69 µs per loop timeit a, b = zip(*bounds) 100000 loops, best of 3: 2.57 µs per loop
itertools.izip is faster yet on mine.

Nils Wagner <nwagner <at> iam.uni-stuttgart.de> writes:
Hi all,
I have a list e.g.
bounds [(1950.0, 2100.0), (1800.0, 1850.0), (1600.0, 1630.0), (1400.0, 1420.0), (1200.0, 1210.0), (990, 1018.0), (10, 12), (12.0, 14.0), (14.0, 16.0), (16.0, 18.0), (18.0, 20)]
How can I extract the first value of each pair given in parenthesis i.e. 1950,1800,1600,1400,... ?
Nils
Its easy i think: bounds_0 = (array(bounds)[:,0]).tolist() volker
participants (5)
-
Gary Ruben
-
Keith Goodman
-
Nils Wagner
-
Paul Rudin
-
volker