比较两个数组及其元素

时间:2020-10-01 04:06:24

标签: python arrays

如何将其隐藏到Python代码中。我不知道如何在玩家和winNum之间进行比较。播放器是2D数组,winNum是1D数组。

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

在功能中,我想达到的目标是,对于每个玩家,我想将其所有数字与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

1 个答案:

答案 0 :(得分:0)

您可以尝试以下代码:

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)