list = ['5', '12', '4', '3', '5', '14', '16', '-2', '4', 'test']
I've a list containing 10 items. How to convert this into a list of integers? I'm getting errors because of 'test' in the list.
list = ['5', '12', '4', '3', '5', '14', '16', '-2', '4', 'test']
I've a list containing 10 items. How to convert this into a list of integers? I'm getting errors because of 'test' in the list.
Don't call a list literally list
. (It shadows the builtin list
function and may give you weird bugs later!)
Assuming you just want to disregard non-numeric values, you can do this in a comprehension by using a filter at the end:
>>> [int(x) for x in [
... '5', '12', '4', '3', '5', '14', '16', '-2', '4', 'test'
... ] if x.lstrip('-').isdigit()]
[5, 12, 4, 3, 5, 14, 16, -2, 4]
supposing you want to forget wrong values, what about
list = ['5', '12', '4', '3', '5', '14', '16', '-2', '4', 'test']
list2 = []
for _ in list:
try:
list2.append(int(_))
except:
pass
print(list2)
execution :
>>> list = ['5', '12', '4', '3', '5', '14', '16', '-2', '4', 'test']
>>> list2 = []
>>> for _ in list:
... try:
... list2.append(int(_))
... except:
... pass
...
>>> print(list2)
[5, 12, 4, 3, 5, 14, 16, -2, 4]
>>>
str.isnumeric
won't pass a negative signstr.lstrip
to remove the -
, check .isnumeric
, and convert to int
if it is.str.isdigit
in place of .isnumeric
.list
)t = ['5', '12', '4', '3', '5', '14', '16', '-2', '4', 'test']
t = [int(v) if v.lstrip('-').isnumeric() else v for v in t]
print(t)
# output
[5, 12, 4, 3, 5, 14, 16, -2, 4, 'test']
t = ['5', '12', '4', '3', '5', '14', '16', '-2', '4', 'test']
t = [int(v) for v in t if v.lstrip('-').isnumeric()]
print(t)
# output
[5, 12, 4, 3, 5, 14, 16, -2, 4]
t = [['5', '12', '4', '3', '5', '14', '16', '-2', '4', 'test'] for _ in range(3)]
t = [[int(v) if v.lstrip('-').isnumeric() else v for v in x] for x in t]
print(t)
# output
[[5, 12, 4, 3, 5, 14, 16, -2, 4, 'test'],
[5, 12, 4, 3, 5, 14, 16, -2, 4, 'test'],
[5, 12, 4, 3, 5, 14, 16, -2, 4, 'test']]
Use int()
to convert the strings and use .isnumeric()
to make sure the string represents a number.
str_arr = ['5', '12', '4', '3', '5', '14', '16', '-2', '4', 'test']
int_arr = []
for s in str_arr:
if s.strip('-').isnumeric():
int_arr.append(int(s))
print(int_arr)
Use the following code:
list = ['5', '12', '4', '3', '5', '14', '16', '-2', '4', 'test']
print([int(i) for i in list if i.lstrip('+-').isdigit()])
Which will give the following output:
[5, 12, 4, 3, 5, 14, 16, -2, 4]
You are getting error because alphabets cannot be converted to integers. However integers maybe converted to strings.
You may solve you issue by using 'try' and 'except'
I will give you how it can be done
for i in range(len(list)):
try:
list[i] = int(list[i])
except:
print(i,'cannot be converted to string')
The try and except statements work just like if and else statements. If there is no error in the code then 'try' statement will work. Whenever there is an error, except statement will get into action.
You can imagine it as:
if (no_error): #try statement
do_these_things
elif (any_error): #except statement
do_these_instead
Please try the following code:
list = ['5', '12', '4', '3', '5', '14', '16', '-2', '4', 'test']
list1=[]
for i in list:
if(i.isnumeric()==True):
list1.append(i)
else:
continue
print(list1)