0
Follow
0
View

Python randomly matching two arrays, e.g. Owners and Gifts

qweasd911aca 注册会员
2023-01-25 20:59

You can use random.shuffle() to shuffle one of your lists, then you can zip() them. This should solve the issue without having to worry about memory.

import random
people = ['tom', 'dick', 'harry']
gifts = ['toms', 'dicks', 'harrys']
random.shuffle(gifts)
results = dict(zip(people, gifts))
print(results)
inu1120 注册会员
2023-01-25 20:59

Assuming that no one gets more than one gift (if they can you might check out itertools.product). The trick is for every person find any "valid receivers" to apply random.choice() to. The main criteria seems to be that you can't give to yourself or to the person who gave to you:

I'm sure there are going to be more efficient solutions, but this will get you started:

import random

givers = ['tom', 'dick', 'harry', "joe"]
receivers  = givers.copy()
receivers_givers = {}
for giver in givers:
    # ---------------------
    # You can't give to yourself or to the person who gave to you.
    # ---------------------
    valid_receivers  = [reciever for reciever in receivers  if reciever not in (giver, receivers_givers.get(giver))]
    # ---------------------

    receiver = random.choice(valid_receivers)
    receivers.remove(receiver)
    receivers_givers[receiver] = giver

for reciever, giver in receivers_givers.items():
    print(f"{giver} gifts to {reciever}")

That should give you something like:

tom gifts to joe
dick gifts to harry
harry gifts to tom
joe gifts to dick
cxmhaoya 注册会员
2023-01-25 20:59

This should work for you.

import random

people = ['tom', 'dick', 'harry', 'arne']
gifts = ['toms', 'dicks', 'harrys', 'arne']

# assign each person a gift randomly but not at the
# same index and don't repeat the same gifts twice so remove them

gift_dict = {}
for people_index in range(len(people)):
    gift_choice = people_index
    while gift_choice == people_index:
        gift_choice = gifts.index(random.choice(gifts))
    gift_dict[people[people_index]] = gifts[gift_choice]
    gifts.pop(gift_choice)

print(gift_dict)

output

{'tom': 'harrys', 'dick': 'arne', 'harry': 'dicks', 'arne': 'toms'}

About the Author

Question Info

Publish Time
2023-01-25 20:59
Update Time
2023-01-25 20:59