下面是使用两个循环的小的解决方案和一个条件语句(仅有3行):
>>> playerdeck = ['Five of Spades', 'Eight of Spades',
... 'Eight of Clubs', 'Four of Clubs', 'Ace of Spades',
... 'Eight of Hearts', 'Four of Diamonds']
>>> cards = ['King', 'Queen', 'Jack', 'Ace',
... 'Two', 'Three', 'Four', 'Five',
... 'Six', 'Seven', 'Eight', 'Nine',
... 'Ten']
>>> for pd in playerdeck:
... for card in cards:
... if card in pd:
... print card
...
Five
Eight
Eight
Four
Ace
Eight
Four
或者,如果你想用尝试列表理解:
>>> playerdeck = ['Five of Spades', 'Eight of Spades',
... 'Eight of Clubs', 'Four of Clubs', 'Ace of Spades',
... 'Eight of Hearts', 'Four of Diamonds']
>>> cards = ['King', 'Queen', 'Jack', 'Ace',
... 'Two', 'Three', 'Four', 'Five',
... 'Six', 'Seven', 'Eight', 'Nine',
... 'Ten']
>>> result = [card for pd in playerdeck for card in cards if card in pd]
>>> result
['Five', 'Eight', 'Eight', 'Four', 'Ace', 'Eight', 'Four']
当然,但在实际的代码中,playerdeck是来自另一个列表的7个随机对象的列表。在操作中,我只是简化了一切。 –
看看答案。 –