Skip to main content
 首页 » 编程设计

python之仅将列表中的某些单词分配给变量

2025年02月15日852php

我正在尝试将 playerdeck 列表中 cards 列表中的单词分配给一个变量。这是我尝试使用的代码,但它返回 False。

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'] 
 
new = cards in playerdeck 
print(new) 

有人可以帮忙吗?

请您参考如下方法:

你可以试试:

>>> 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: 
    temp = pd.split(" ") 
    for data in temp: 
        if data in cards: 
            print data 
 
 
Five 
Eight 
Eight 
Four 
Ace 
Eight 
Four