How do you turn the numbers on the list into numbers?
-
Let's say we have a list:
r = ['Alica', '884', 'Bread', '231']
We need to convert the numbers into numbers, and we don't have rows.
-
Is that the option?
r = ['Alica', '884', 'Bread', '231']
n = [int(i) if i.isnumeric() else i for i in r]
print(n)
The truth is, it only works with natural numbers, which are only numerical.
a more universal method:
r = ['Alica', '884', 'Bread', '-231', '2.1e5', '3.1415']
n = []
for i in r:
# пробуем преобразовать в float
try:
value = float(i)
n.append(int(value) if value == int(value) else value)
except ValueError:
n.append(i)print(n)