Andreas
Andreas

Reputation: 61

how to get a list of integers?

The situation is:

a1= ("5.6,13.16,19,23,24,25,26,29,30,31,32,34,35,36,49,50,51,54,60,65,67,74,75,76,77,81,86,87").replace (".",",")
#replace . with comma
print (a1)

output is :

5,6,13,16,19,23,24,25,26,29,30,31,32,34,35,36,49,50,51,54,60,65,67,74,75,76,77,81,86,87

Now, i would like insert a1 in the list of integer, like this:

[5,6,13,16,19,23,24,25,26,29,30,31,32,34,35,36,49,50,51,54,60,65,67,74,75,76,77,81,86,87]

Any suggestion?

Thanks

Upvotes: 0

Views: 38

Answers (2)

user4374390
user4374390

Reputation:

You can just use the split like follows:

list = a1.split (",")
# convert elements to ints
li = []
for i in list:
    li.append(int(i))
# print list as integers
print "li : ", li

Upvotes: 1

Manu mathew
Manu mathew

Reputation: 989

You could split up the string like and then convert each element in the list to integer if you need it as an integer string.

[int(a) for a in a1.split(',')]

Upvotes: 1

Related Questions