How to Pick "X" Number of Unique Numbers from a List in Python

How do you pick x number of unique numbers from a list in Python?

That's exactly what random.sample() does.

>>> random.sample(range(1, 16), 3)
[11, 10, 2]

Edit: I'm almost certain this is not what you asked, but I was pushed to include this comment: If the population you want to take samples from contains duplicates, you have to remove them first:

population = [1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1]
population = list(set(population))
samples = random.sample(population, 3)

How to get a unique value from a list in python

Count the items, keep only those which have a count of 1:

>>> data = ['a','a','b','a']
>>> from collections import Counter
>>> [k for k,v in Counter(data).items() if v == 1]
['b']

Generate 'n' unique random numbers within a range

If you just need sampling without replacement:

>>> import random
>>> random.sample(range(1, 100), 3)
[77, 52, 45]

random.sample takes a population and a sample size k and returns k random members of the population.

If you have to control for the case where k is larger than len(population), you need to be prepared to catch a ValueError:

>>> try:
... random.sample(range(1, 2), 3)
... except ValueError:
... print('Sample size exceeded population size.')
...
Sample size exceeded population size

I need to generate x amount of unique lists inside another list

Pulling 7 of 34 numbers from your numberrange without repeats can be done using random.sample - to ensure you do not get duplicate lists, you can add a tuple of the list to a set and your final result and only add to final if this tuple is not yet in the set:

import random

numbers = range(1, 35) # 1...34

final = []
chosen = set()
while len(final) < 10:
# replace popping numbers with random.sample
one_list = random.sample(numbers, k=7) # 7 numbers out of 34 - no repeats
# create a tuple of this list and only add to final if not yet added
t = tuple(one_list)
if t not in chosen:
chosen.add(t)
final.append(one_list)

print (final)

Output:

[[1, 5, 10, 26, 14, 33, 6], 
[3, 11, 1, 30, 7, 21, 18],
[24, 23, 28, 2, 13, 18, 1],
[4, 25, 32, 15, 22, 8, 27],
[32, 9, 10, 16, 17, 26, 12],
[34, 32, 10, 26, 16, 21, 20],
[6, 34, 22, 11, 26, 12, 5],
[29, 17, 25, 15, 3, 6, 5],
[24, 8, 31, 28, 17, 12, 15],
[6, 19, 11, 22, 30, 33, 15]]

If you dont need unique resulting lists, you can simplify this to a one-liner but it might have dupes inside:

final = [random.sample(range(1,11),k=7) for _ in range(10)]

How to make lists contain only distinct element in Python?

The simplest is to convert to a set then back to a list:

my_list = list(set(my_list))

One disadvantage with this is that it won't preserve the order. You may also want to consider if a set would be a better data structure to use in the first place, instead of a list.

How do I take UNIQUE random choice from two lists

First, combine the marks and values to actual cards using itertools.product, then just random.shuffle the stack of cards and pop cards into the player's hand, like you would in real life.

import itertools, random
cards = list(itertools.product(values, marks))
random.shuffle(cards)
hand = [cards.pop() for _ in range(12)]

At the same time, this will remove the cards from the stack, and thus ensure that cards are unique across the different players' hands. If you do not want this, use random.sample instead. This way, cards are unique within one hand, but not across hands, as they remain in the stack (in this case, the shuffle step is not needed, either):

hand = random.sample(cards, 12)

Get unique values in List of Lists

array = [['a','b'], ['a', 'b','c'], ['a']]
result = {x for l in array for x in l}

How to get unique values from list in specific order?

This should do it:

L1 = ['r-A','r-G','S1','r-A','S2','r-O','r-G','S2','S1','r-A']
L2 = []
for item in L1:
if 'r-' in item and item in L2:
continue
else:
L2.append(item)
print(L2)

Output:

['r-A', 'r-G', 'S1', 'S2', 'r-O', 'S2', 'S1']


Related Topics



Leave a reply



Submit