已通过 Stack Exchange 进行了一些搜索,回答了问题,但无法找到我要查找的内容。
给定以下列表:
a = [1, 2, 3, 4]
我将如何创建:
a = ['hello1', 'hello2', 'hello3', 'hello4']
谢谢!
已通过 Stack Exchange 进行了一些搜索,回答了问题,但无法找到我要查找的内容。
给定以下列表:
a = [1, 2, 3, 4]
我将如何创建:
a = ['hello1', 'hello2', 'hello3', 'hello4']
谢谢!
另一种选择是使用内置的地图功能:
a = range(10)
map(lambda x: 'hello%i' % x, a)
根据 WolframH 评论编辑:
map('hello{0}'.format, a)
使用列表理解:
In [1]: a = [1,2,3,4]
In [2]: ["hello" + str(x) for x in a]
Out[2]: ['hello1', 'hello2', 'hello3', 'hello4']
你也可以使用%
而不是format()
>>> a = [1, 2, 3, 4]
>>> ['hello%s' % i for i in a]
['hello1', 'hello2', 'hello3', 'hello4']