Thursday, 25 January 2018

python - How to convert integers in a list to int()?

I'm making a function that evaluates maths in a string. First, it takes the string and then converts it to a list.



I tried converting the entire array into an array of integers, but I run into an error when the array looks like this: ["hello",1,"*",2] as everything isn't a number.



I want to only convert the integers in the array ["1","2","hello","3"] to integers, so the array becomes [1,2,"hello",3]



That way, I can do maths on the integers and not have them treated as strings, as currently, when I do this:



1 + 2



I get 12 as the output.
I want 3 as the output.

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...