Python 仅将列表中的某些单词指定给变量
我正在尝试将列表Python 仅将列表中的某些单词指定给变量,python,list,Python,List,我正在尝试将列表卡中列表playerdeck中的单词分配给一个变量。这是我试图使用的代码,但它返回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
卡中列表playerdeck
中的单词分配给一个变量。这是我试图使用的代码,但它返回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)
有人能帮忙吗?课程卡:
class Card:
def __init__(self,value):
self.value = value
def __eq__(self,other):
return str(other) in self.value
def __str__(self):
return self.value
def __repr__(self):
return "<Card:'%s'>"%self
def __hash__(self):
return hash(self.value.split()[0])
playerdeck = map(Card,['Five of Spades', 'Eight of Spades',
'Eight of Clubs', 'Four of Clubs', 'Ace of Spades',
'Eight of Hearts', 'Four of Diamonds'] )
cards = set(['King', 'Queen', 'Jack', 'Ace',
'Two', 'Three', 'Four', 'Five',
'Six', 'Seven', 'Eight', 'Nine',
'Ten'])
print cards.intersection(playerdeck)
定义初始值(自身,值):
自我价值=价值
定义(自身、其他):
在self.value中返回str(其他)
定义(自我):
回归自我价值
定义报告(自我):
返回“%self”
定义散列(自我):
返回散列(self.value.split()[0])
playerdeck=地图(牌,[‘黑桃五’、‘黑桃八’,
“八支梅花”、“四支梅花”、“黑桃王牌”,
“八颗红心”、“四颗钻石”])
卡片=套装(['King'、'Queen'、'Jack'、'Ace',
‘二’、‘三’、‘四’、‘五’,
‘六’、‘七’、‘八’、‘九’,
“十”])
打印卡片。交叉点(playerdeck)
试试这个,它首先遍历这些卡,并检查哪些卡在玩家牌堆中。如果有匹配项,它会将其附加到新卡上并转到下一张卡
new = []
for card in cards:
for deck in player_deck:
if card.lower() in deck.lower():
new.append(card)
break
您可以尝试:
>>> 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
下面是使用两个循环和一个条件语句(仅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']
>>> result = [card for pd in playerdeck for card in cards if card in pd]
>>> result
['Five', 'Eight', 'Eight', 'Four', 'Ace', 'Eight', 'Four']
当然,但在我的实际代码中,playerdeck是另一个列表中7个随机对象的列表。在op中,我只是简化了它。看看答案。
>>> 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']