Creating random pairs from lists
Solution 1
Using random.randrange
to select (and remove) a random element from a list is easy:
def pop_random(lst):
idx = random.randrange(0, len(lst))
return lst.pop(idx)
Now, assuming that the list has an even number of elements, we can build pairs very easily:
pairs = []
while lst:
rand1 = pop_random(lst)
rand2 = pop_random(lst)
pair = rand1, rand2
pairs.append(pair)
There are 2 steps missing that I'll leave as an exercise for you:
- Make sure the list is unique before you start
- Make sure the unique list has an even number of elements (and figure out what to do if it doesn't ...)
Solution 2
import random
family = ["Mother", "Father", "Aunt", "Uncle", "Brother", "Sister" ]
pairs = {}
for p in range(len(family) // 2):
pairs[p+1] = ( family.pop(random.randrange(len(family))),
family.pop(random.randrange(len(family))) )
print(pairs)
Solution 3
import random
l = ["Mother", "Father", "Aunt", "Uncle", "Brother", "Sister" ]
pairs = {}
while len(l) > 1:
#Using the randomly created indices, respective elements are popped out
r1 = random.randrange(0, len(l))
elem1 = l.pop(r1)
r2 = random.randrange(0, len(l))
elem2 = l.pop(r2)
# now the selecetd elements are paired in a dictionary
pairs[elem1] = elem2
#The variable 'pairs' is now a dictionary of this form:
#{'Sister': 'Aunt', 'Uncle': 'Father', 'Mother': 'Brother'}
##We can now print the elements of the dictionary in your desired format:
i = 1
for key, value in pairs.items():
print("Pair {}: {} and {}".format(i, key, value))
i += 1
When you run it, you should see something like this:
Pair 1: Sister and Aunt
Pair 2: Mother and Brother
Pair 3: Uncle and Father
Solution 4
If you want to create unique pairs, you can shuffle the indexes of the array and then iterate through the shuffled array every 2 elements. If the number of elements in the array is even you're done.
import numpy as np
l = ["Mother", "Father", "Aunt", "Uncle", "Brother", "Sister" ]
indexes = list(range(0, len(l)))
np.random.shuffle(indexes) # please note that shuffle is inplace
pairs = {}
for i in range(0, len(indexes), 2):
pairs[l[i]] = l[i+1]
i = 1
for key, value in pairs.items():
print(f'Pair {i}: {key} and {value}')
i += 1
This is the result:
Pair 1: Mother and Father
Pair 2: Aunt and Uncle
Pair 3: Brother and Sister
Tony Garangean
Updated on July 24, 2022Comments
-
Tony Garangean almost 2 years
I am trying to create a program that will print pairs of the elements in a list. I need to create a dictionary (that is empty at first) where I can store values, Loop through the list to make a pair and make sure there are no duplicates.
When I loop in the list, I need to get a random number and I can use it to remove an element. Remove the randomly selected element from the list using pop method, store the element to a variable, say element1. Repeat it to create element2.
Map the element1 to element2 by inserting the element1 as key to the pairs dictionary, and setting its value to element2, that is, if we call pairs[element1] later it should give us the value of element2.
Print the result using dictionary’s items() and keys() methods.
The catch is, the only function we are allowed is the random.randrange()from the random module :(
Example is this:
list = ["Mother", "Father", "Aunt", "Uncle", "Brother", "Sister" ]
Sample run of the program, this creates 3 pairs because there are 6 elements in the list.
Pair 1: Mother and Aunt Pair 2: Uncle and Sister Pair 3: Brother and Father
Here is my program for now:
family = ["Mother", "Father", "Aunt", "Uncle", "Brother", "Sister" ] for x in family: pairs = {}
How can I improve/add to this code?