如何获取整数列表?

时间:2019-05-10 11:55:29

标签: list python-3.7

情况是:

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)

输出为:

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

现在,我想在整数列表中插入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]

有什么建议吗?

谢谢

2 个答案:

答案 0 :(得分:1)

您可以像这样分割字符串,然后将列表中的每个元素转换为整数(如果需要将其作为整数字符串)。

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

答案 1 :(得分:1)

您可以按如下方式使用拆分:

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