(May-24-2019, 05:24 PM)ichabod801 Wrote: You are getting a list out of random.sample, not an integer. Therefore it's not in the list of integers you are checking against, and you get a list of lists rather than a list of integers. Put [0] at the end of line 15 to get the integer out.
Is there some reason you are not just making a list that excludes the outcasts and taking a sample from that?
valid = [x for x in whole_list if x not in outcast_list] selection_list = random.sample(valid, 5)
Thanks for the prompt reply and explanation, mate! I also just realized this returning "list" instead of "int" issue, a few minutes ago, while debugging. As I'm new to Python and not well versed to coding in "short form" like yours, I amended my code as follows.
import random # The "outcast_list" list contains numbers to be filtered off. outcast_list = [1, 2, 3, 4, 5, 6] # The "whole_list" list contains numbers that the generated number shall randomly choose from. whole_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] selection_list = [] status = False # Create a loop until the "status" turns True (i.e. when the list of randomized numbers has 5 numbers inside it.). while status is False: # Generate one random from the "whole_list" one at a time. i_list = random.sample(whole_list, 1) i = int(i_list[0]) # Create the following 3 conditions before adding the number into the "selection_list" list. if (i not in outcast_list) and (i not in selection_list) and (status is False): selection_list.append(i) # Break out of the "while" loop when the "selection_list" has 5 numbers inside it. if len(selection_list) == 5: status = True break print(selection_list)