How to create random pairs from list
You can use random.shuffle
and create a generator to get samples of image groups as many times as needed.
def image_generator(l):
while True:
random.shuffle(l)
yield l[:2]
for i in range(8):
print(next(image_generator(imageList)))
['6.jpg', '2.jpg']
['10.jpg', '5.jpg']
['8.jpg', '6.jpg']
['6.jpg', '1.jpg']
['5.jpg', '3.jpg']
['8.jpg', '6.jpg']
['6.jpg', '2.jpg']
['8.jpg', '2.jpg']
Another way is you can use itertools.product
to get permutations of n (or 2) for images, filtering out cases where both images are the same. And then you can use random.sample
to get 8 samples.
import random
import itertools
def image_generator(iterable, groups, samplesize):
grouped = (i for i in itertools.product(imageList,repeat=2) if i[0]!=i[1])
return random.sample(list(grouped), samplesize)
image_generator(imageList, 2, 8)
[('8.jpg', '7.jpg'),
('7.jpg', '10.jpg'),
('7.jpg', '2.jpg'),
('6.jpg', '7.jpg'),
('5.jpg', '3.jpg'),
('2.jpg', '1.jpg'),
('10.jpg', '5.jpg'),
('9.jpg', '10.jpg')]
forming random pairs from a list (sort of...)
I assume that since you want to select people at random, your choice of list provides fast random access. A simple solution is just to shuffle the whole list, and just pair people off in pairs from the front of the list.
The Fisher-Yates shuffle is a quick, easy way to shuffle the list randomly.
Then you can just run through and pair them off:
for x from 0 to persons.size(), x += 2
pair(persons.get(i), persons.get(i + 1));
Since the elements are unique, you won't have to worry about people getting paired twice or with themselves.
Also be careful to make sure your list has an even number of people first! If the total is odd you'll have to deal with the extra person at the end of the list somehow.
How to create a randomly matched list of pairs for an odd number of variables without replacement?
Draw one less pair if the list length is odd. This will leave three elements instead of one element - which are then, by definition, the leftover size-three "pair".
pairs=np.random.choice(x, size=(fruit_count // 2 - 1, 2), replace=False)
You can quickly remove the fruits already in size-two pairs by using set operations.
pair_size_three = list(set(x) - set.union(*(set(pair) for pair in pairs)))
Edit: Just realized there is a faster alternative by doing the above in reverse. First pick three elements from the collection and remove them, then group the remaining elements into pairs. This saves a lot of set operations if the list is long.
three_elements = np.random.choice(x, size=(1, 3), replace=False)
remaining_elements = list(set(x) - set(three_elements))
pairs = np.random_choice(remaining_elements, size=(len(remaining_elements) // 2, 2), replace=False)
Randomly selecting a different pair of items from a list
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']]
Related Topics
Pandas - How to Compare 2 CSV Files and Output Changes
Replacing Values in a Dataframe for Given Indices
How to Split an Array According to Conditional Statement
Python Regex - Finding Phone Number
How to Create Dynamic Workflows in Airflow
Algorithm for Distributing a Number Between Certain Number of Chunks
Change CSV Name to CSV Date Time Python
Convert String to Negative Number
Why Is This Going Out of Range
Run Multiple Python File Concurrently
Python - How to Separate Paragraphs from Text
Python/Regex - How to Extract Date from Filename Using Regular Expression
How to Change Dd-Mm-Yyyy Date Format to Yyyy-Dd-Mm in Pandas
Faster Way to Read Excel Files to Pandas Dataframe
Background Color for Tk in Python