![]() |
Create random pairs - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Create random pairs (/thread-10691.html) |
Create random pairs - Dennisp44 - May-31-2018 I am trying to create random pairings from a list of players. I first need to randomize the entire list into pairs and then divide that list into halves. I then want to calculate which of the fist group of pairings will play against the others. example. List 1,2,3,4,5,6,7,8,9,10 Random Pairs 2 and 4, 1 and 3,5 and 9, 10 and 7, 6 and 8, Then divide this list in two and create new pair of pairs Example 2 and 4 play 1 and 3, 5 and 9 play 10 and 7 and so on..... I have worked out the first pairs, but am stuck on the second part. How do I complete the code? import random # Input the actual number of players registered to play today num_players = int(input("How many players are there today? ")) # Add 1 to num_players to make the number correct num_players += 1 # Create the list of players from "1" to number entered in input my_list = list(range(1, num_players)) l = my_list # Create pairs 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 selected elements are paired in a dictionary pairs[elem1] = elem2 #The variable 'pairs' is now a dictionary of randomised pairs ##We can now print the elements of the dictionary in your desired format: i = 1 for key, value in pairs.items(): # Print out the result print("Rink {:2d}: {:6d} and {:3d}".format(i, key, value)) i += 1 RE: Create random pairs - buran - Jun-01-2018 Not sure what 2 and 4 play 1 and 3 mean, but in my opinion you don't need 2-step procedure. selecting each 4 elements in random is enough. then first pair play the second pairimport random players = set(range(1,21)) while players: pairs = random.sample(players,4) print(pairs) players -= set(pairs) # an alternative print('\n\nNow the alternative') players = range(1,21) random.shuffle(players) while players: pairs=[players.pop() for _ in range(4)] print (pairs) if we use your language 19 and 7 play 14 and 16
RE: Create random pairs - Dennisp44 - Jun-02-2018 The randomizing of pairs is just what I need. Thanks. However, the output needs to be in the format of my original code output as I have to allocate rink numbers to the pairings. The people using this output would not understand a list of 4 elements. I would need to print it as Rink 1: 19 and 7 play 14 and 16 This output would then be used manually by the organisers. Is there a way I can modify your code to present the data this way? I am a novice at Python and appreciate any ideas. RE: Create random pairs - buran - Jun-02-2018 import random def draw(num_players): players = set(range(1,num_players+1)) while players: pairs = random.sample(players,4) players -= set(pairs) yield pairs for rink, pairs in enumerate(draw(num_players=20), start=1): print('Rink {}: {} and {} play {} and {}'.format(rink, *pairs)) Actually, let's make it in more proper way: import random def draw(num_players): players = set(range(1,num_players+1)) while players: pairs = random.sample(players,4) players -= set(pairs) yield pairs if __name__ == '__main__': while True: try: num_players = int(input('How many players are there today? ')) if num_players % 4: print('Number of players must be divisible by 4') else: break except ValueError: print('This is not a valid input') for rink, pairs in enumerate(draw(num_players=num_players), start=1): print('Rink {}: {} and {} play {} and {}'.format(rink, *pairs))
|