I have a list in txt file and I want to remove the quotation marks from the numbers. For example the list:
['1023', '1025', '1028']
change to
[1023, 1025, 1028]
How do I do it in Python?
I have a list in txt file and I want to remove the quotation marks from the numbers. For example the list:
['1023', '1025', '1028']
change to
[1023, 1025, 1028]
How do I do it in Python?
We can use a list comprehension here along with int()
to convert each string in the list to an integer.
inp = ["1023", "1025", "1028"]
output = [int(x) for x in inp]
print(output) # [1023, 1025, 1028]
The easiest way is probably:
Your list:
a = ['1023','1025','1028']
And change it to a NumPy array:
import numpy as np
a = np.array(a).astype(float)
Out: array([1023., 1025., 1028.])
Or astype(int)
depending on if you want items after the decimal point.
Out: array([1023, 1025, 1028])