【问题标题】:How to create a dictionary out of random values from two lists? [closed]如何从两个列表中的随机值创建字典? [关闭]
【发布时间】:2021-03-09 03:19:07
【问题描述】:

上下文:我有两个大小不等的列表; names 包含家庭成员的姓名,chores 包含更长的清单,嗯,家务。我正在编写一个程序,将每个家务随机分配给一个家庭成员,以便每个人都获得相同数量的家务(或至少 +1/-1)。至少在理论上,我已经想到了一些可能的方法来解决这个问题。一种方法是简单地打乱chores 的列表,将列表平均分成n 新列表,然后将这些较小的列表中的一个分配给每个家庭成员。我还可以遍历家务列表,在每次通过时为每个家庭成员分配一个家务,直到所有家务都分配给一个家庭成员。

我很难找到具体的操作或示例来帮助解决这个问题;我应该考虑一个特定的工作流程吗?

【问题讨论】:

  • “最简单”的方法是简单地将所有家务分配给您最讨厌的家庭成员并声称“运气不好”...
  • 顺便说一句,我建议您应该为测试提供示例数据。
  • 哦,回答标题问题:是的,当然有可能。这只会使标题混乱。
  • @Manuel 你真的和所有的 cmets 一起流泪了。我不认为我要求任何人提供他们自己的样本数据。
  • 我在这里不知所措,并且很困惑为什么这个问题被关闭了。更不用说在我们已经找到一个可行的解决方案之后关闭它。如果问题确实需要额外的“细节或清晰度”,我们就不会得出一个解决方案。由于我们有一个可行的解决方案,多亏了这些出色的贡献者,我不会编辑或删除问题,这可能对其他人有价值。

标签: python pandas macos dataframe


【解决方案1】:

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

import random

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

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

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']}

(第一次尝试,Ashley 真的很幸运。)

【讨论】:

  • @Nick 我认为最好将所有人都包含在字典中。
  • 好点,我应该考虑到阿什利可能会再次走运! :)
  • 如果我不清楚,我很抱歉,但家务需要尽可能平均分配!抱歉,没有人能不做一些家务就逃过一劫!!
  • @Manuel 是否可以将每个家庭成员的最小家务数量设置为家务数量除以家庭成员数量的商?
  • @HoganMarhoefer 当然可以,但是为什么呢?解决方案 1 已经实现了这一点。
【解决方案2】:

您可以使用zip + cycle 或zip_longest 将人与家务配对,具体取决于您的家务事少还是人少。如果你的家务活比人多,这仍然会创建一个包含所有人的 DataFrame,其中一些人以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

【讨论】:

    【解决方案3】:

    这是一种方法:

    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)
    

    【讨论】:

    • 我注意到两件事情...似乎八个“家务”中的两个在您的输出中丢失了,并且“割草”在输出中出现了两次,但在杂务中只出现了一次列表。
    • @HoganMarhoefer 查看我修改后的答案。
    • 关于第二个解决方案:可怜的约翰,总是第一个(甚至可能只是)得到剩余的家务。不公平。
    • @Manuel 我更新了答案并解决了余数为零的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-03
    • 2015-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    相关资源
    最近更新 更多