Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python - name of np array variable as string

I have this code:

I restated the problem formulation somewhat. Not sure if etiquette is to post a new question. If so, my bad

import numpy as np
a = np.array([0,0])
b = np.array([1,0])
c = np.array([2,0])

my_array_list = [a,b,c]
for my_array in my_array_list:
    np.save('string_that is representative of my_array',my_array)

where 'string that is representative of my array' equals to 'a', 'b', 'c' or similar.

There is likely a better way to solve this, I can just not come up with one right now.

What is the best way to do this, fast and efficiently?

like image 801
Alejandro Simkievich Avatar asked Jan 24 '16 20:01

Alejandro Simkievich


People also ask

Can NumPy arrays be strings?

The elements of a NumPy array, or simply an array, are usually numbers, but can also be boolians, strings, or other objects. When the elements are numbers, they must all be of the same type.

How do you print an array name in Python?

To print an array in Python, use the print() function. The print() is a built-in Python function that takes the name of the array containing the values and prints it. To create an array in Python, use the numpy library and create an array using the np. array() function, and then print that array in the console.

What is NumPy STR_?

numpy. string_ is the NumPy datatype used for arrays containing fixed-width byte strings. On the other hand, str is a native Python type and can not be used as a datatype for NumPy arrays*.

How do you declare a string array in Python?

Introduction to String Array in Python String Array can be defined as the capacity of a variable to contain more than one string value at the same time, which can be called and accessed at any time in the program during the execution process.


1 Answers

I can suggest you that function:

import numpy as np

def namestr(obj, namespace):
    return [name for name in namespace if namespace[name] is obj]

my_numpy = np.zeros(2)
print(namestr(my_numpy, globals()))

With output:

['my_numpy']
like image 111
George Petrov Avatar answered Oct 20 '22 07:10

George Petrov