[英]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.