Python - 将每个整数转换为列表中的字符串并添加逗号

时间:2014-11-02 02:14:30

标签: python string list comma

我是python的新手。我需要创建一个从1到70的整数列表,但对于每个整数,我想让它成为一个字符串和一个逗号,并将其存储在另一个列表中。

例如:

for i in range (1,71):

    list_of_ints.append(i)

{ Some code 

}

它应该是这样的

columns = ['1','2','3','4'.......'70']

4 个答案:

答案 0 :(得分:1)

使用[str(i) for i in range(1, 71)]。这会为str(i)中的所有i提供range(1, 71)列表。函数str(i)返回i作为str值,而不是int

答案 1 :(得分:0)

好像你想要这样的东西,

>>> l = []
>>> for i in range(1,71):
    l.append(str(i))
>>> l
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70']

答案 2 :(得分:0)

new_list = [str(x) for x in range(1, 71)]

使用列表推导来实现相同的结果。

答案 3 :(得分:0)

您可以使用map来帮助您:

>>> list_of_ints = range(1, 71)
>>> list_of_ints = map(str, list_of_ints)
>>> print list_of_ints
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70']
>>>