有没有办法将字符串附加到每个列表的第一个位置?
Is there an way to appending a string to each list on first position?
所以我遇到的问题是,我有一个列表(其中包含所有元素),我想将一个字符串(例如 'watch')附加到列表中的每个元素,但在第一个位置。
list = ['sample', 'test', 'hello']
string = 'watch '
# the result should like this:
# ['watch sample', 'watch test', 'watch hello']
我试过好几次了。但是,它总是给出错误信息。
有办法解决这个问题吗?
谢谢。
l = ['sample', 'test', 'hello']
string = 'watch '
output = [string + i for i in l]
print(output)
>>> ['watch sample', 'watch test', 'watch hello']
请注意,我使用 l
而不是 list
。一般来说,你不应该超载这样的内置术语。
所以我遇到的问题是,我有一个列表(其中包含所有元素),我想将一个字符串(例如 'watch')附加到列表中的每个元素,但在第一个位置。
list = ['sample', 'test', 'hello']
string = 'watch '
# the result should like this:
# ['watch sample', 'watch test', 'watch hello']
我试过好几次了。但是,它总是给出错误信息。 有办法解决这个问题吗? 谢谢。
l = ['sample', 'test', 'hello']
string = 'watch '
output = [string + i for i in l]
print(output)
>>> ['watch sample', 'watch test', 'watch hello']
请注意,我使用 l
而不是 list
。一般来说,你不应该超载这样的内置术语。