how to use the name of a ndarray as a string
Hi, Does anyone know how I can quickly use the name of a ndarray as a string? for example, I have In [54]: index=np.arange(100) then I want to use the name 'index' as a key in a new dictionary: d3=dict() d3['index']=index I can do it like the way above, but I have many ndarray variables that need to be included in the dictionary. is there something like: d3[index.name()]=index while index.name() would equal the string 'index'? I hope my question is clear. thanks to all. Chao -- *********************************************************************************** Chao YUE Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL) UMR 1572 CEA-CNRS-UVSQ Batiment 712 - Pe 119 91191 GIF Sur YVETTE Cedex Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16 ************************************************************************************
In such a situation you should probably use a dictionary from the start,
i.e.:
d3['index'] = np.arange(100)
then use d3['index'] everywhere instead of index.
It can be more convenient (notation-wise) to use an object instead, i.e.
either work within a class method (self.index = np.arange(100)), or use a
class as storage instead of a dictionary:
class ArrayHolder(object):
pass
a = ArrayHolder()
a.index = np.arange(100)
...
d3 = a.__dict__.copy()
Finally, another option I can see is to use a custom class that holds both
a numpy array and a name. Maybe with a convenient accessor to the array
through the call method:
class NamedArray(object):
def __init__(self, array, name):
self.array = array
self.name = name
def __call__(self):
return self.array
index = NamedArray(numpy.arange(100), 'index')
... # use index() in the rest of the code whenever you want the numpy
array
d3[index.name] = index()
-=- Olivier
2011/11/10 Chao YUE
Hi,
Does anyone know how I can quickly use the name of a ndarray as a string? for example, I have In [54]: index=np.arange(100)
then I want to use the name 'index' as a key in a new dictionary: d3=dict() d3['index']=index
I can do it like the way above, but I have many ndarray variables that need to be included in the dictionary. is there something like: d3[index.name()]=index while index.name() would equal the string 'index'?
I hope my question is clear. thanks to all.
Chao --
*********************************************************************************** Chao YUE Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL) UMR 1572 CEA-CNRS-UVSQ Batiment 712 - Pe 119 91191 GIF Sur YVETTE Cedex Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16
************************************************************************************
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
On 11/10/11 3:57 AM, Olivier Delalleau wrote:
In such a situation you should probably use a dictionary from the start,
all good suggestions, but while we're at it: On 11/10/11 2:17 AM, Chao YUE wrote:
Does anyone know how I can quickly use the name of a ndarray as a string?
This reflects a key misunderstanding of how Python works -- a ndarray ( or any python object) does not "have a name". when you write: index=np.arange(100) you are creating a ndarray object, and _binding_ the name "index" to it. the object exists apart from it's name: l =[] l.append(np.arange(100)) now there is a an andarray object in the list -- but it has no name. You can also bind multiple names to the same object: index=np.arange(100) fred = index john = index now "index", "fred", and "john" are all bound to the same object -- what would be its name now? Hence Olivier's suggestionss -- al various ways to store and refere to objects -- binding to a variable is only one way. This is a good read that should clarify it all: http://python.net/~mwh/hacks/objectthink.html -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
participants (3)
-
Chao YUE
-
Chris.Barker
-
Olivier Delalleau