How to convert String into integers in python sickit-learn

Question:

I have a list as above:

probs= ['2','3','5','6']

and i want to convert those string to a numeric values like the following result:

resultat=[2, 3, 4, 5, 6]

I tried some solutions appearing on this link:
How to convert strings into integers in Python?
such as this one:

new_list = list(list(int(a) for a in b) for b in probs if a.isdigit())

but it didn’t work, someone can help me to adapt this function on my data structure, i will be really thankful.

Asked By: Ha KiM's

||

Answers:

Use int() and list comprehension iterate the list and convert the string values to integers.

>>> probs= ['2','3','5','6']
>>> num_probs = [int(x) for x in probs if x.isdigit()]
>>> num_probs
[2, 3, 5, 6]
Answered By: Haifeng Zhang
>>> probs= ['2','3','5','6']
>>> probs= map(int, probs)
>>> probs
[2, 3, 5, 6]

or (as commented):

>>> probs= ['2','3','5','6']
>>> probs = [int(e) for e in probs]
>>> probs
[2, 3, 5, 6]
>>> 
Answered By: Billal Begueradj

if your list is as above, you don’t need to check if the value is a number.
something like

probs = ["3","4","4"];
resultat = [];

for x in probs:
    resultat.append(int(x))

print resultat

would work

Answered By: pointbazaar
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.