0
Q:

python string to list of int

# initializing list  
test_list = ['1', '4', '3', '6', '7'] 

# using list comprehension to 
# perform conversion 
test_list = [int(i) for i in test_list]
1
>>> example_string = '0, 0, 0, 11, 0, 0, 0, 0, 0, 19, 0, 9, 0, 0, 0, 0, 0, 0, 11'
>>> list(map(int, example_string.split(',')))  # Python 3, in Python 2 the list() call is redundant
[0, 0, 0, 11, 0, 0, 0, 0, 0, 19, 0, 9, 0, 0, 0, 0, 0, 0, 11]
>>> [int(s) for s in example_string.split(',')]
[0, 0, 0, 11, 0, 0, 0, 0, 0, 19, 0, 9, 0, 0, 0, 0, 0, 0, 11]
0
test_list = ['1', '4', '3', '6', '7'] 
  
# Printing original list 
print ("Original list is : " + str(test_list)) 
  
# using naive method to 
# perform conversion 
for i in range(0, len(test_list)): 
    test_list[i] = int(test_list[i]) 
      
  
# Printing modified list  
print ("Modified list is : " + str(test_list)) 
-1
[int(s) for s in example_string.split(',')]
0

New to Communities?

Join the community