It's impossible to do this. The Python interpreter automatically converts numbers like 0004
to 4
.
The only way to do this is by converting everything to a string. If you want to do maths with the content of your array you convert it back to float.
arr = [
[4907., 4907., 4907.],
[4907., 4907., 4907.],
[4907., 4907., 4907.]
]
new_arr = []
for i in range(0, len(arr)):
new_arr.append([])
for j in range(0, len(arr)):
nr = arr[i][j]
new_arr[i].append(str(nr).zfill(len(str(nr)) + 3))
print(new_arr)
Output:
[['0004907.0', '0004907.0', '0004907.0'], ['0004907.0', '0004907.0', '0004907.0'], ['0004907.0', '0004907.0', '0004907.0']]
Edit:
However, if you have to use this array a lot, the most elegant way to achieve this is to make a class in my opinion. That would feel more natural and you won't have to convert between strings and float each time. Thus being faster as well.
#Special class
class SpecialArray:
#Your array
arr = [
[4907., 4907., 4907.],
[4907., 4907., 4907.],
[4907., 4907., 4907.]
]
#Append leading zero's when class is initiated
def __init__(self):
temp_arr = []
for i in range(0, len(self.arr)):
temp_arr.append([])
for j in range(0, len(self.arr)):
nr = self.arr[i][j]
temp_arr[i].append(str(nr).zfill(len(str(nr)) + 3))
self.arr = temp_arr
#Print out array
def print(self):
print(self.arr)
#Get a value to to math
#If asString is true, you get back the string with leading zero's (not for math)
def get(self, x, y, asString = False):
if not asString:
return float(self.arr[x][y])
else:
return self.arr[x][y]
#TODO: Make function to append etc here
###Rest of your program
def main():
#Initiate your array
arr = SpecialArray()
#Print out whole array
arr.print()
#Output:
#[['0004907.0', '0004907.0', '0004907.0'], ['0004907.0', '0004907.0', '0004907.0'], ['0004907.0', '0004907.0', '0004907.0']]
#Print out one element
print(arr.get(1, 2, True))
#Output:
#0004907.0
#Get one element and increase by one (do math)
x = arr.get(1,2) + 1
print(x)
#Output:
#4908.0
main()