0

Given the input

Guess='1 5K'

and a list

playable=['AC','QS','5S','5K','KC']

How would I go about determining if the '5K' part of Guess is in playable? Also, if it is in playable, how would I go about determining if the item 1 spot to the left of it has either a '5' or a 'K' in it. So playable would become

playable=['AC','QS','5K','KC']

With '5K' replacing '5S'. So far I have

def oneC(Guess):
    split_em=Guess.split() ##splits at space
    card_guess=split_em[1] ##gets the '5K' part of string
    if card_guess is in playable:
                                          ##Confusion Area
        index1=playable.index(card_guess) ##Finds index of '5K'
        index_delete= del playable[index1-1] ##Deletes item to the left of '5K'

My problem right now is i'm not sure how to determine if '5' or 'K' is in the item one spot to the left. Is there a way to turn '5K' into a set of ('5','K') and then turn the element one spot to the left to ('5','S') and run intersection on them? I wrote this on my phone because my internet is down at my house so sorry for any confusion or misspellings. Thanks for your time!

4

1 回答 1

0

您可以遍历左侧项目中的字符,看看是否有“5”或“K”:

index1 = playable.index(card_guess)
if any(c in playable[index1 - 1] for c in card_guess):
    del playable[index1]

请注意,上面的代码不会检查这种情况,index1 == 0因此您必须定义当猜测的卡片是列表中的第一张卡片时会发生什么。

因此,要回答您的问题:for c in card_guess迭代其中的字符"5K"(它们是'5''K',然后检查其中是否有任何字符在playable[index1 - 1],这是左侧的项目"5K")。

于 2013-05-08T18:54:59.483 回答