This question already has an answer here:
I'm trying to create a function that generates a list of random integers, then allows user input to tell the function to randomly select some number of integers from the list, and then create a user-specified "sub-list" (e.g. breaks the list of selected numbers into smaller lists of 2, 3, 4, 5...etc.)
For instance, the user will tell the function to create a list of 100 random integers, the user will input to select 10 of those integers at random, and then the user will input to create a sub-list that arranges integers by some number (e.g. 2,3,4, etc). Here's what I have so far:
import random
random_list = range(101) # the random list of numbers goes here
num_to_select = 10 # choose the number of integers to select from list
list_of_random_items = random.sample(random_list, num_to_select)
first_random_item = list_of_random_items[0]
So I can get the random list of integers to generate and allow the user to input the range of the list and how many integers to randomly select. However, I'm not sure how to create additional functionality to handle the sub-lists.
The current output looks something like this:
[48, 69, 66, 88, 90, 0, 68, 82, 14, 36]
I want to add an additional step to let the user to arrange that output list into a sub-list of, for example, 2:
[[68, 0], [14, 36], [48, 90], [69, 82], [66, 88]]
or a sub-list of 5:
[[68, 0, 14, 36, 48], [90, 69, 82, 66, 88]]
I imagine the input should look something like this: ([48, 69, 66, 88, 90, 0, 68, 82, 14, 36], 2)
or like this ([48, 69, 66, 88, 90, 0, 68, 82, 14, 36], 5)
, respectively.
What's a clean way of allowing user input to break the random list into a smaller sub-list?
Aucun commentaire:
Enregistrer un commentaire