Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating a numpy array filled with a character

Is there an alternative way to create a numpy array filled with a character, e.g.:

p = np.array( [' '] * 12 )

Is there a way to use np.full(...)?

like image 951
ErikR Avatar asked Sep 18 '15 17:09

ErikR


People also ask

How do I create a NumPy array with a specific shape?

Use the numpy. ones() function to create a numpy array of a specified shape that is is filled with the value one (1).

How do I fill an array in NumPy?

fill() method is used to fill the numpy array with a scalar value. If we have to initialize a numpy array with an identical value then we use numpy. ndarray. fill().

Can a NumPy array contains string?

The elements of a NumPy array, or simply an array, are usually numbers, but can also be boolians, strings, or other objects.

How do I create an array of strings in NumPy?

Starting from numpy 1.4, if one needs arrays of strings, it is recommended to use arrays of dtype object_ , string_ or unicode_ , and use the free functions in the numpy. char module for fast vectorized string operations.


1 Answers

Yes np.full could be used with the correct datatype (string) being mentioned with it, like so -

np.full((12), [' '],dtype=str)

You can also use np.repeat -

np.repeat([' '], 12)
like image 158
Divakar Avatar answered Sep 30 '22 13:09

Divakar