简体   繁体   中英

How do you make n number of lists with k number of integers in python?

I'm trying to make a lottery program where you can play multiple "rows". I have managed to make it in a way that you can pick one row of numbers and the program picks the winning numbers and checks the results.

So the question is how can the player choose multiple sets of numbers? All numbers have to be unique in a particular set but different sets can have duplicates.

For example this is okay: [1,2,4,6,9], [1,2, 5,11,16]

This isn't [1,1,2,3,3], [4,4,6,8,11]

This can be achieved using the random.sample() function.

import random

# Set the number of rows
number_of_rows = 3

# Set how many numbers needed for each row
count = 5

# Set the possible numbers for the lottery
numbers = [x for x in range(50)]

# Empty result array
result = []

# Take a sample of the numbers for each of the desired rows
for row in range(0, number_of_rows):
    result += [random.sample(numbers, count)]

# Print result
print(result)

Note that this example returns the desired number of rows and their numbers as an array of arrays. There are of course other ways to achieve this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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