python - 有没有办法将字符串附加到第一个位置的每个列表?
问题描述
所以我遇到的问题是,我有一个列表(其中包含所有元素)并且我想将一个字符串(例如'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
. 一般来说,你不应该用这样的术语来重载。
推荐阅读
- google-sheets-formula - google sheet vlookup from another sheet (same doc) #N/A
- electron - 如何与电子中本机`window.open`打开的子窗口共享相同的分区?
- python - Pandas Groupby 获得最多多列但按顺序排列
- javascript - 需要一个连续加载网页的“下一步”按钮/链接
- java - Java polymorphshim:对象数组
- javascript - 在播放和播放以及暂停和暂停图像之间淡入淡出
- java - Wildfly 25:开始出现错误 java.lang.NoClassDefFoundError: org/jboss/security/RunAs
- android - 如何使用颤振使用 video_player 动态更改视频?
- hibernate - 带有@ColumnDefault 的 CommandAcceptanceException
- r - While循环将值存储在向量中并在向量中已经存在值时停止[R编程]