简体   繁体   中英

Randomly selecting a different pair of items from a list

Code:

import random

x = ['A','B','C','D','E','F',
     'G','H','I','J','K','L',
     'M','N','O','P','Q','R',
     'S','T','U','V','W','X',
     'Y','Z']

y1 = random.sample(x, 2)
y2 = random.sample(x, 2)
y3 = random.sample(x, 2)
y4 = random.sample(x, 2)
y5 = random.sample(x, 2)

Query

As shown above, I'm selecting 5 random sample combinations and declaring them under the variables y'x' .

To improve my code, I would like to do so, but ensure that an item from the list doesn't appear more than once in all variable outputs, in which all combinations are different and non-repetitive. I would preferably like to achieve this without having to remove items from the list as it is reused later in the code.

Expected Output (Example):

>>> y1
['A', 'Q']
>>> y2
['E', 'K']
>>> y3
['C', 'O']
>>> y4
['Z', 'X']
>>> y5
['P', 'L']

You could shuffle a copy of the list (you said you wanted to reuse it so one needs to make a copy because shuffle works in-place) and then just take 2 elements for each sample:

import random

x_copy = x[:]  # copy
random.shuffle(x_copy)
y1 = x[:2]
y2 = x[2:4]
y3 = x[4:6]
y4 = x[6:8]
y5 = x[8:10]

or if you don't want to hardcode the yi s:

x_copy = x[:]  # copy
random.shuffle(x_copy)
y = [x_copy[i*2: (i+1)*2] for i in range(5)]
print(y)
# [['W', 'Z'], ['A', 'Q'], ['B', 'J'], ['O', 'D'], ['X', 'E']]

You can use numpy.random.choice . Its purpose is to choose with ( replace=True ) or without ( replace=False ) replacement from an array-like object (which also works for your list):

import numpy as np
x = ['A','B','C','D','E','F',
     'G','H','I','J','K','L',
     'M','N','O','P','Q','R',
     'S','T','U','V','W','X',
     'Y','Z']
np.random.choice(x, size=(5, 2), replace=False)

Result:

array([['Y', 'Q'],
       ['W', 'R'],
       ['O', 'H'],
       ['Z', 'G'],
       ['L', 'M']], 
      dtype='<U1')

This returns an array of 5 rows, which each include one of your samples of size 2.

You could simply build a 'cache' of generated values - so the elements of x are not removed:

import random

class SampleCache():
    x = ['A','B','C','D','E','F',
         'G','H','I','J','K','L',
         'M','N','O','P','Q','R',
         'S','T','U','V','W','X',
         'Y','Z']

    def __init__(self):
        self.cache = []

    def get(self):
        _iterations = 0
        while 1:
            sample = random.sample(self.x, 2)
            if not sample in self.cache:
                self.cache.append(sample)
                return sample

            if _iterations > 1000: # just to prevent NOT to run into an infinite loop
                break


s = SampleCache()
for x in range(25):
    print(s.get())

random.sample is the correct method, you just need to call it once for 10 letters instead of 5 times with 2 letters:

import random
import string


def random_letters(m=5, n=2):
    letters = random.sample(string.ascii_uppercase, m * n)
    return [letters[n * i:n * (i + 1)] for i in range(m)]

print(random_letters())
# [['I', 'X'], ['J', 'U'], ['O', 'W'], ['G', 'C'], ['D', 'F']]
print(random_letters())
# [['J', 'X'], ['N', 'P'], ['A', 'C'], ['O', 'Z'], ['B', 'H']]
print(random_letters())
# [['U', 'T'], ['J', 'N'], ['C', 'H'], ['D', 'I'], ['K', 'P']]
print(random_letters())
# [['U', 'G'], ['L', 'V'], ['A', 'R'], ['J', 'F'], ['S', 'C']]
print(random_letters())
# [['Y', 'C'], ['R', 'B'], ['E', 'I'], ['S', 'T'], ['H', 'X']]

Use random.sample to generate a shuffled copy of the initial list, and a generator to yield the shuffled values as required.

def random_sample(x, n):
    shuffled = random.sample(x, k=len(x))
    for val in range(0, len(x), n):
        yield shuffled[val: val+n]

print([sample for sample in random_sample(x, 2)])

Outputs;

[['I', 'O'], ['V', 'T'], ['U', 'J'], ['L', 'A'], 
 ['E', 'G'], ['Q', 'F'], ['M', 'H'], ['B', 'K'], 
 ['R', 'P'], ['W', 'N'], ['D', 'S'], ['Z', 'Y'], 
 ['X', 'C']]

If you want exactly five random values then use this;

samples = random_sample(x,  2)
five_samples = [next(samples) for _ in range(5)]
print(five_samples)

If want them one at a time then use,

samples = random_sample(x,  2)
print(next(samples))
...
print(next(samples))

You can loop over the sample generated and remove the elements from x :

x = ['A','B','C','D','E','F',
 'G','H','I','J','K','L',
 'M','N','O','P','Q','R',
 'S','T','U','V','W','X',
 'Y','Z']

new_x = x[:]

import random
final_list = []
for i in range(5):
   the_sample = random.sample(new_x, 2)
   final_list.append(the_sample)
   for b in the_sample:
       new_x.remove(b)

Output:

[['C', 'R'], ['L', 'V'], ['W', 'Y'], ['D', 'O'], ['J', 'Q']]

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