How to convert list of strings to integer in python

In my python Script, I have:

user = nuke.getInput("Frames Turned On") userLst = [user] print userLst 

Result:

 ['12,33,223'] 

I was wondering how to remove ' in a list or somehow convert it to int?

+8
python string list int
source share
6 answers

Use split() to separate by commas, use int() to convert to an integer:

 user_lst = map(int, user.split(",")) 
+19
source share

Not listed for deletion. When you print a list because it does not have a direct string representation, Python shows you its repr -a line, which shows its structure. You have a list with one item, line 12,33,223 ; what does [user] .

You probably want to separate the string with commas, for example:

 user_list = user_input.split(',') 

If you want them to be int s, you can use the understanding:

 user_list = [int(number) for number in user_input.split(',')] 
+9
source share
 [int(s) for s in user.split(",")] 

I have no idea why you defined a separate userLst variable, which is a list of one element.

+1
source share
 >>> ast.literal_eval('12,33,223') (12, 33, 223) 
+1
source share
 >>> result = ['12,33,223'] >>> int(result[0].replace(",", "")) 1233233 >>> [int(i) for i in result[0].split(',')] [12, 33, 233] 
-one
source share

You can use the join method and convert it to an integer:

 int(''.join(userLst)) 

1233223

-one
source share

All Articles