[Numpy-discussion] RecArray.tolist() suggestion

Francesc Alted falted at pytables.org
Tue Jul 13 02:07:00 CDT 2004


A Dimarts 13 Juliol 2004 10:28, Francesc Alted va escriure:
> A Dilluns 12 Juliol 2004 23:14, Perry Greenfield va escriure:
> > What I'm wondering about is what a single element of a record array
> > should be. Returning a tuple has an undeniable simplicity to it.
> 
> Yeah, this why I'm strongly biased toward this possibility.
> 
> > On the other hand, we've been using recarrays that allow naming the
> > various columns (which we refer to as "fields").  If one can refer
> > to fields of a recarray, shouldn't one be able to refer to a field
> > (by name) of one of it's elements? Or are you proposing that basic
> > recarrays not have that sort of capability (something added by a
> > subclass)?
> 
> Well, I'm not sure about that. But just in case most of people would like to
> access records by field as well as by index, I would advocate for the
> possibility that the Record instances would behave as similar as possible as
> a tuple (or dictionary?). That include creating appropriate __str__() *and*
> __repr__() methods as well as __getitem__() that supports both name fields
> and indices. I'm not sure about whether providing an __getattr__() method
> would ok, but for the sake of simplicity and in order to have (preferably)
> only one way to do things, I would say no.

I've been thinking that one can made compatible to return a tuple on a
single element of a RecArray and still being able to retrieve a field by
name is to play with the RecArray.__getitem__ and let it to suport key names
in addition to indices. This would be better seen as an example:

Right now, one can say:

>>> r=records.array([(1,"asds", 24.),(2,"pwdw", 48.)], "1i4,1a4,1f8")
>>> r._fields["c1"]
array([1, 2])
>>> r._fields["c1"][1]
2

What I propose is to be able to say:

>>> r["c1"]
array([1, 2])
>>> r["c1"][1]
2

Which would replace the notation:

>>> r[1]["c1"]
2

which was recently suggested.

I.e. the suggestion is to realize RecArrays as a collection of columns,
as well as a collection of rows.

-- 
Francesc Alted





More information about the Numpy-discussion mailing list