unzip array of arrays?

T

Tobiah

Although it's trivial to program, I wondered whether
there was a builtin or particularly concise way to
express this idea:
a = [(1, 2), (3, 4), (5, 6)]
field[a, 2]
[2, 4, 6]

where field() is some made up function.

Thanks,

Toby
 
C

Chris Rebert

Although it's trivial to program, I wondered whether
there was a builtin or particularly concise way to
express this idea:
a = [(1, 2), (3, 4), (5, 6)]
field[a, 2]
[2, 4, 6]

where field() is some made up function.

Python 2.6 (r26:66714, Nov 18 2008, 21:48:52)
[GCC 4.0.1 (Apple Inc. build 5484)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
a= [(1, 2), (3, 4), (5, 6)]
zip(*a) [(1, 3, 5), (2, 4, 6)]
zip(*a)[1]
(2, 4, 6)

Cheers,
Chris
 
R

Robert Kern

Unknown said:
Although it's trivial to program, I wondered whether
there was a builtin or particularly concise way to
express this idea:
a = [(1, 2), (3, 4), (5, 6)]
field[a, 2]
[2, 4, 6]

where field() is some made up function.

The above example is a great application for numpy's array
type:

http://numpy.scipy.org/

Here's an example showing how you extract a column using
indexing operations:

http://www.scipy.org/Tentative_NumPy_Tutorial#head-864862d3f2bb4c32f04260fac61eb4ef34788c4c

[I assume numpy is still the numerical/array package du jour?]

Yes.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
T

Tobiah

Although it's trivial to program, I wondered whether
there was a builtin or particularly concise way to
express this idea:
a = [(1, 2), (3, 4), (5, 6)]
field[a, 2]
[2, 4, 6]

where field() is some made up function.

Python 2.6 (r26:66714, Nov 18 2008, 21:48:52)
[GCC 4.0.1 (Apple Inc. build 5484)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
a= [(1, 2), (3, 4), (5, 6)]
zip(*a) [(1, 3, 5), (2, 4, 6)]
zip(*a)[1]
(2, 4, 6)

That would be what I was after. Where can I read about
this mysterious use of the '*'? It only works in the
context of the zip() function. It's hard to understand
how the interpreter handles that.

Thanks,

Toby
 
C

Chris Rebert

Although it's trivial to program, I wondered whether
there was a builtin or particularly concise way to
express this idea:

a = [(1, 2), (3, 4), (5, 6)]
field[a, 2]
[2, 4, 6]

where field() is some made up function.

Python 2.6 (r26:66714, Nov 18 2008, 21:48:52)
[GCC 4.0.1 (Apple Inc. build 5484)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
a= [(1, 2), (3, 4), (5, 6)]
zip(*a) [(1, 3, 5), (2, 4, 6)]
zip(*a)[1]
(2, 4, 6)

That would be what I was after. Where can I read about
this mysterious use of the '*'? It only works in the
context of the zip() function. It's hard to understand
how the interpreter handles that.

It works in all functions.

Basically:
f(*[1,2,3]) === f(1,2,3)

I'm sure someone will also point out the docs.

Cheers,
Chris
 
S

Steve Holden

Bryan said:
Hmmm... that's a harder question than I thought. Am I missing it, or
does Python's doc need a write-up of the extended call syntax?
No, you aren't mistaken. Looking at the "*" symbol in the 2.6
documentation index it lists only two references. The first is the
language manual's explanation of its use in the def statement, the
second is a transitory reference to its use in function calls, but
that's in the tutorial where it is not likely to get much attention.
It works generally, for any callable. See the doc for 'apply':

http://docs.python.org/library/functions.html#non-essential-built-in-functions


It not only works in a call, but also in function definitions. You can
call a function with the extended call syntax whether or not is defined
with * and ** arguments.
regards
Steve
 
S

Steve Holden

Bryan said:
Hmmm... that's a harder question than I thought. Am I missing it, or
does Python's doc need a write-up of the extended call syntax?
No, you aren't mistaken. Looking at the "*" symbol in the 2.6
documentation index it lists only two references. The first is the
language manual's explanation of its use in the def statement, the
second is a transitory reference to its use in function calls, but
that's in the tutorial where it is not likely to get much attention.
It works generally, for any callable. See the doc for 'apply':

http://docs.python.org/library/functions.html#non-essential-built-in-functions


It not only works in a call, but also in function definitions. You can
call a function with the extended call syntax whether or not is defined
with * and ** arguments.
regards
Steve
 
M

Mark Wooding

Steve Holden said:
No, you aren't mistaken. Looking at the "*" symbol in the 2.6
documentation index it lists only two references. The first is the
language manual's explanation of its use in the def statement, the
second is a transitory reference to its use in function calls, but
that's in the tutorial where it is not likely to get much attention.

There's a full description of it in 5.4.3 in the Language Reference, but
apparently not indexed.

-- [mdw]
 
B

Bryan Olson

Mark said:
There's a full description of it in 5.4.3 in the Language Reference, but
apparently not indexed.

So I guess this means I can duck out of writing up a lecture on my own
understanding of Python's extended call syntax. Great.

I think I grock the extended call syntax, and when the question came up
I was surprised not to be able to find where I learned it. Mark, where
exactly does one look to see this "full description"?
 
M

Mark Wooding

Bryan Olson said:
So I guess this means I can duck out of writing up a lecture on my own
understanding of Python's extended call syntax. Great.

I think I grock the extended call syntax, and when the question came
up I was surprised not to be able to find where I learned it. Mark,
where exactly does one look to see this "full description"?

I typoed, sorry! I should have written 5.3.4:

http://www.python.org/doc/2.5/ref/calls.html

The description of the fancy * and ** syntax starts with `If the syntax
``*expression'' appears in the function call...'.

-- [mdw]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,299
Messages
2,571,544
Members
48,295
Latest member
Adriene271

Latest Threads

Top