可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Suppose I have a list of suits of cards as follows:
suits = ["h","c", "d", "s"]
and I want to add a type of card to each suit, so that my result is something like
aces = ["ah","ac", "ad", "as"]
is there an easy way to do this without recreating an entirely new list and using a for
loop?
回答1:
This would have to be the 'easiest' way
>>> suits = ["h","c", "d", "s"] >>> aces = ["a" + suit for suit in suits] >>> aces ['ah', 'ac', 'ad', 'as']
回答2:
Another alternative, the map function:
aces = map(( lambda x: 'a' + x), suits)
回答3:
If you want to add something different than always 'a' you can try this too:
foo = ['h','c', 'd', 's'] bar = ['a','b','c','d'] baz = [x+y for x, y in zip(foo, bar)] >>> ['ha', 'cb', 'dc', 'sd']