简体   繁体   English

一维数组和二维数组元素搜索

[英]1D array and 2D array element searching

I'm creating a lottery program where I generate everything using random number and store them in a 1D array and a 2D array.我正在创建一个彩票程序,我使用随机数生成所有内容并将它们存储在一个一维数组和一个二维数组中。 The first array(1D) contains 8 winning numbers sorted into 2 groups called g1 and g2.第一个数组(1D)包含 8 个中奖号码,分为 2 个组,称为 g1 和 g2。 The second array(2D) contains the players and their numbers.第二个数组(2D) 包含球员和他们的号码。

For each player from the 2D array I need to determine if they are winners or not by comparing them with the winning numbers.对于二维数组中的每个玩家,我需要通过将他们与中奖号码进行比较来确定他们是否中奖。 For example if any numbers from the players is also in the winning numbers i need to count them and determine if they are winners of what class.例如,如果来自玩家的任何数字也在中奖号码中,我需要计算它们并确定他们是否是哪个级别的赢家。

Their are four classes class 1 - players with all 6 winning numbers class 2 - players with any 5 winning numbers class 3 - players with any 4 winning numbers class 4 - players with any 3 winning numbers or players with less than 3 numbers from g1 group and has both numbers from the g2 group他们有四个班级 1级 - 6个中奖号码的玩家 2级 - 任何5个中奖号码的玩家 3级 - 任何4个中奖号码的玩家 4级 - g1组中任何3个中奖号码的玩家或少于3个号码的玩家并且有来自 g2 组的两个数字

if not then they lose如果不是那么他们输了

also if the player enters his/her ID it should return the players numbers and also if they are winner or not with the class(1,2,3,4)此外,如果玩家输入他/她的 ID,它应该返回玩家号码以及他们是否是班级的获胜者(1,2,3,4)

playerID = 2
playerID = input("Please enter player ID ")
if playerID is in a winner it should return 
     playerID
     playerNum
     winner or not with the class

issubset can help you match array elements. issubset可以帮助您匹配数组元素。 Do keep in mind that issubset does not require the order of the elements to be the same.请记住, issubset不要求元素的顺序相同。 But exactly that's why I recommended this - because in lottery the order should not matter.但这正是我推荐这个的原因——因为在彩票中,顺序应该无关紧要。

Solution= [1,2,3,4] 
userGuess= [1,2,3,4]
otherGuess= [2,3,4]
badGuess= [4,5,6]

if set(userGuess).issubset(set(Solution)):
    return True // TRUE

if set(otherGuess).issubset(set(Solution)):
    return True // TRUE

if set(badGuess).issubset(set(Solution)):
    return True // FALSE

You can check length of the array to match if you don't want shorter subsets to be valid.如果您不希望较短的子集有效,您可以检查数组的长度以匹配。

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

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