[英]appending to the strings within a list of strings in python
so i saw this as an answer to a question from a year ago about adding/extending a string, etc. 所以我认为这是一年前有关添加/扩展字符串等问题的答案。
s = 'foo'
s += 'bar'
s += 'baz'
l = []
l.append('foo')
l.append('bar')
l.append('baz')
my question is how would one combine these two features? 我的问题是如何结合这两个功能? l would return:
我会返回:
['foo','bar','baz']
but what if i wanted to add a letter to the end of each string in the list and then have it return: 但是,如果我想在列表中每个字符串的末尾添加一个字母,然后返回,该怎么办:
['food','bars','bazy']
is this a thing or is it more wishful thinking? 这是事情还是一厢情愿的事情?
I'm not sure I understand. 我不确定我是否理解。 Are you looking for something like this:
您是否正在寻找这样的东西:
first_list = ['foo','bar','baz']
second_list = [x+y for x,y in zip(first_list,'dsy')]
you can use zip()
along with join()
and map()
. 您可以将
zip()
与join()
和map()
。
In [72]: lis=['foo','bar','baz']
In [73]: map("".join,zip(lis,'dsy'))
Out[73]: ['food', 'bars', 'bazy']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.