繁体   English   中英

将两个数组及其元素相互比较

[英]Compare two arrays and their elements against one another

我如何将其转换为 Python 代码。 我不知道如何在玩家和 winNum 之间进行比较。 玩家是一个二维数组,winNum 是一个一维数组。

下面是我一直在使用的示例数据集

在函数中,我想要实现的是,对于玩家中的每个玩家,我想将他们的所有数字与 winNum 数字进行比较,并找出它们是否匹配,如果它们匹配,我想对计数进行 1 增量。 两个数组都有 8 个元素,它们从 0-6 和 6-8 排序。

winNum = [0, 5, 20, 22, 23, 25, 0, 26]
player = [[0, 5, 20, 22, 23, 25, 0, 26],[14, 15, 21, 25, 26, 29, 30, 30],[3, 6, 8, 16, 25, 30, 0, 13]]
for i in  range (len(player)):
    for j in range(6):
            x = player[i][j]
            and compare with winNum[](0-6)
            if x is in winNum:
                count +=c1
                and move on to the next number
            else
                ignore and move on to the next number

你可以试试下面的代码:

count=0
for i in range(len(player)):
    for j in range(8):
        x = player[i][j]
        if x in winNum:
            count+=1

您还可以通过将“计数”附加到新的空列表来进一步存储它。 我不确定你是否也想要那样,但这样做也很简单。

count_list = []
for i in range(len(player)):
    count=0
    for j in range(8):
        x = player[i][j]
        if x in winNum:
            count+=1
    count_list.append(count)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM