I´m trying to generate an array with integers of a fixed length of 6, so that e.g. 1234 is displayed as 001234
and 12345 as 012345
.
I can get it to work for an integer using:
JavaScript
x
5
1
x = 12345
2
x = '{0:06d}'.format(x)
3
print(x)
4
>>> 012345
5
I tried the same method for an array, but it doesn`t seem to work, so how can I convert this method to array entries?
JavaScript
1
7
1
dummy_array = np.array([1234, 653932, 21394, 99999, 1289])
2
for i in range(len(dummy_array)
3
dummy_array[i] = '{0:06d}'.format(dummy[i])
4
5
print(dummy_array[2]) #test
6
>>>21394
7
Do I need convert the array entries to strings first?
Advertisement
Answer
If you set:
JavaScript
1
2
1
dummy_array = np.array([1234, 653932, 21394, 99999, 1289],dtype=object)
2
it allows the numpy array to contain integers and strings simultaneously, which was your problem. With this simple dtype argument your code will work.