如何从两个列表中的随机值创建字典?

2024-04-27 00:06:42 发布

您现在位置:Python中文网/ 问答频道 /正文

上下文:我有两个大小不等的列表names拥有家庭成员的名字chores拥有更长的家务清单。我正在编写一个程序,随机将每个家务分配给一个家庭成员,这样每个人都可以得到相同数量的家务(或者至少+1/-1)。我想到了一些可能的方法,至少在理论上是这样。一种方法是简单地洗牌chores列表,将列表均匀地分割成n个新列表,并将其中一个较小的列表分配给每个家庭成员。我还可以循环浏览家务清单,在每次通过时为每个家庭成员分配家务,直到所有家务都分配给一个家庭成员

我很难找到具体的操作或例子来帮助解决这个问题;是否有一个具体的工作流程我应该考虑?p>


Tags: 方法程序列表数量names流程理论名字
3条回答

从pakpe的答案中窃取的设置:

import random

names = ['John', 'Ashley', 'Debbie']
chores = ['cook', 'clean', 'shop', 'pay bills', 'wash car', 'mow lawn', 'walk dog', 'drive kids']

解决方案1,平均分配家务:

random.shuffle(names)
random.shuffle(chores)
assignments = {name: chores[i::len(names)]
               for i, name in enumerate(names)}

样本结果:

{'Debbie': ['wash car', 'clean', 'shop'],
 'Ashley': ['walk dog', 'mow lawn', 'cook'],
 'John': ['drive kids', 'pay bills']}

解决方案2,可能稍微简单但不均匀(在他们更改问题之前发布):

assignments = {name: [] for name in names}
for chore in chores:
    assignments[random.choice(names)].append(chore)

样本结果:

{'Debbie': ['mow lawn', 'pay bills', 'shop', 'cook'],
 'Ashley': [],
 'John': ['wash car', 'walk dog', 'drive kids', 'clean']}

(第一次尝试,艾希礼真的很幸运。)

这里有一个方法:

import random
names = ['John', 'Ashley', 'Debbie']
chores = ['cook', 'clean', 'shop', 'pay bills', 'wash car', 'mow lawn', 'walk dog', 'drive kids']

ratio = len(chores)//len(names)
assignments = {}
for name in names:
    assignments[name] = random.sample(chores, ratio)

print(assignments)

#{'John': ['mow lawn', 'pay bills'], 'Ashley': ['cook', 'walk dog'], 'Debbie': ['mow lawn', 'shop']}

如果您希望分配所有杂务,而不希望重复杂务,以下是一个解决方案:

import random
names = ['John', 'Ashley', 'Debbie']
chores = ['cook', 'clean', 'shop', 'pay bills', 'wash car', 'mow lawn', 'walk dog', 'drive kids']

random.shuffle(chores)
chunck = len(chores)//len(names)
remainder = len(chores)%len(names)

#distribute even chunks of chores amongst names
assignments = {}
for idx, name in enumerate(names):
    assignments[name] = chores[idx*chunck: idx*chunck+chunck]

#distribure remaining chores amongst names
if remainder != 0:
    remaining_chores = chores[-remainder:]
    for i in range(len(remaining_chores)):
        assignments[names[i]] += [remaining_chores[i]]
    
print(assignments)

您可以使用zip+cyclezip_longest将人与家务配对,这取决于您的家务活是少还是人少。如果你的家务活比人多,这仍然会创建一个所有人的数据框架,有些人的家务活是None

import pandas as pd
from itertools import cycle, zip_longest
from random import shuffle

names = ['John', 'Ashley', 'Debbie']
chores = ['cook', 'clean', 'shop', 'pay bills', 'wash car', 'mow lawn', 'walk dog', 'drive kids']

shuffle(chores)
shuffle(names)

if len(names) <= len(chores):
    data = zip(cycle(names), chores)
else:
    data = zip_longest(names, chores)

df = pd.DataFrame(data, columns=['name', 'chore'])

#     name       chore
#0    John    wash car
#1  Ashley    mow lawn
#2  Debbie       clean
#3    John    walk dog
#4  Ashley  drive kids
#5  Debbie        shop
#6    John        cook
#7  Ashley   pay bills

现在你可以用pandas做任何你想做的事情,比如聚合到一个列表

df.groupby('name').chore.agg(list)
#name
#Ashley              [wash car, cook, clean]
#Debbie                     [shop, mow lawn]
#John      [walk dog, drive kids, pay bills]
#Name: chore, dtype: object

相关问题 更多 >