Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在列表列表中查找匹配的前导值_Python - Fatal编程技术网

Python 在列表列表中查找匹配的前导值

Python 在列表列表中查找匹配的前导值,python,Python,我有一份清单。它们一开始是空列表,在我的程序中,我通过在前面插入值来添加到列表中。这是一个纸牌游戏。类似于纸牌游戏。因此,列表以a开头,以国王结尾。我现在要做的是确定玩家何时真正赢得了比赛,这意味着国王是名单中每个名单的第一张牌。我做这件事有困难。目前,当我循环使用我的函数时,一旦一个列表具有king值,它就会让我知道我赢了 BANNER = WINNER!! foundations = [ [], [], [], [] ] for lst in foundations[:]: for

我有一份清单。它们一开始是空列表,在我的程序中,我通过在前面插入值来添加到列表中。这是一个纸牌游戏。类似于纸牌游戏。因此,列表以a开头,以国王结尾。我现在要做的是确定玩家何时真正赢得了比赛,这意味着国王是名单中每个名单的第一张牌。我做这件事有困难。目前,当我循环使用我的函数时,一旦一个列表具有king值,它就会让我知道我赢了

BANNER = WINNER!!
foundations = [ [], [], [], [] ]

for lst in foundations[:]:
    for item in lst:
        if item.rank() == 13: # the rank of a king
            print(BANNER)
            return True
else:
    return False
我要找的是:

when foundations = [ [13], [13], [13], [13] ] # all four list containing king

then the loop returns True

如果item.rank()==13条件为
True
,则问题在于,如果item.rank()

您似乎也在单步遍历所有列表值,但只需要单步遍历每个列表中的第一个值

所以你要找的代码是

for list in foundations:
    if list[0] != 13:
        return False  # this is definitely not a win
return True # this code will only work if return False didn't work

在本例中,我假设每个
列表至少有一个值。根据您的问题,您可能希望绕过此假设。

我认为如果您颠倒逻辑,会更容易。也就是说:如果任何一张榜单不是以国王开头的,你就不会赢。否则,你赢了。谢谢你,这是一个很好的看待它的方式!