简体   繁体   English

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

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

How do i covert this into a Python code.我如何将其转换为 Python 代码。 I dont get how to do the compare between players and the winNum.我不知道如何在玩家和 winNum 之间进行比较。 The players is a 2D array and winNum is a 1D array.玩家是一个二维数组,winNum 是一个一维数组。

below is a sample data set that i have been using下面是我一直在使用的示例数据集

in the function what im trying to achive is that for every player in players i want to compare all their numbers with the winNum numbers and find if they match and if they do match i want to make 1 increment to the count.在函数中,我想要实现的是,对于玩家中的每个玩家,我想将他们的所有数字与 winNum 数字进行比较,并找出它们是否匹配,如果它们匹配,我想对计数进行 1 增量。 Both arrays have 8 elements and they are sorted from 0-6 and 6-8.两个数组都有 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

You could try the following code:你可以试试下面的代码:

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

You could also store the 'count' further by appending it to a new empty list.您还可以通过将“计数”附加到新的空列表来进一步存储它。 I wasn't sure if you wanted that too, but doing that is also straight forward.我不确定你是否也想要那样,但这样做也很简单。

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.

相关问题 Python:比较两个数组的元素 - Python: Compare Elements of two arrays 在一个图形上针对另一个绘制2个数组 - Plotting 2 arrays against another on one graph 使用列表推导比较两个数组的元素 - Using list comprehension to compare elements of two arrays 如何比较两个arrays的元素平方和? - How to compare the sum of the square of the elements of two arrays? 比较两个数组中的元素,并使用python当一个值大于另一个时返回True - Compare elements in two arrays and return True when one value is greater than the other using python Python:比较一个文本文件中的正则表达式模式与另一个文本文件中的行 - Python: Compare regex pattern in one text file against line in another 如何在python中将两个变量与一个字符串进行比较? - How do I compare two variables against one string in python? 将列表中的每个元素与另一个元素进行比较 - Compare each of the elements of a list with another one 如何将一个数据帧的元素与另一个进行比较? - How to compare elements of one dataframe to another? 如何比较 Python 中的两 (2) 个不相等的数据帧并将元素从一个分配到另一个? - How to compare two (2) unequal dataframes in Python and assign elements from the one to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM