在Python中通过元组集分发值

在Python中通过元组集分发值,python,combinations,Python,Combinations,由于封锁,我正在尝试为成对对话创建一个rota 我有一份同事名单 我已经创建了一个天数列表 我想按顺序为每一对分配天数,直到分配完所有对为止 但我不知道如何跟踪是否已经分配了一对,以及如何“跑到同事的尽头” 以下是我所拥有的: import itertools import datetime as dt import numpy as np colleagues = ["C", "S", "D", "I",

由于封锁,我正在尝试为成对对话创建一个rota

我有一份同事名单

我已经创建了一个天数列表

我想按顺序为每一对分配天数,直到分配完所有对为止

但我不知道如何跟踪是否已经分配了一对,以及如何“跑到同事的尽头”

以下是我所拥有的:

import itertools
import datetime as dt
import numpy as np

colleagues = ["C",
              "S",
              "D",
              "I",
              "P"]

colleague_pairs = sorted(list(itertools.combinations(colleagues, 2)))
start_date = dt.datetime.today() + dt.timedelta(days=5)
dates = [start_date]
current_date = dates[-1]
while len(dates) < (len(colleagues) -1):
    new_date = current_date + dt.timedelta(days=1)
    if new_date.weekday() < 5:
        dates.append(new_date)
    current_date = new_date
working_days = [date.strftime('%Y-%m-%d') for date in dates ] 


[('C', 'D'), ('C', 'I'), ('C', 'P'), ('C', 'S'), 
 ('D', 'I'), ('D', 'P'), 
 ('I', 'P'), 
 ('S', 'D'), ('S', 'I'), ('S', 'P')]
['2020-05-04', '2020-05-05', '2020-05-06', '2020-05-07']
我尝试了一整天的迭代,但显然没有成功:

for pair in colleague_pairs:
    for day in working_days:
        print({pair:day})

{('C', 'D'): '2020-05-04'}
{('C', 'D'): '2020-05-05'}
{('C', 'D'): '2020-05-06'}
{('C', 'D'): '2020-05-07'}
{('C', 'I'): '2020-05-04'}
{('C', 'I'): '2020-05-05'}
{('C', 'I'): '2020-05-06'}
{('C', 'I'): '2020-05-07'}
{('C', 'P'): '2020-05-04'}
{('C', 'P'): '2020-05-05'}
{('C', 'P'): '2020-05-06'}
{('C', 'P'): '2020-05-07'}
{('C', 'S'): '2020-05-04'}
{('C', 'S'): '2020-05-05'}
{('C', 'S'): '2020-05-06'}
{('C', 'S'): '2020-05-07'}
{('D', 'I'): '2020-05-04'}
{('D', 'I'): '2020-05-05'}
{('D', 'I'): '2020-05-06'}
{('D', 'I'): '2020-05-07'}
{('D', 'P'): '2020-05-04'}
{('D', 'P'): '2020-05-05'}
{('D', 'P'): '2020-05-06'}
{('D', 'P'): '2020-05-07'}
{('I', 'P'): '2020-05-04'}
{('I', 'P'): '2020-05-05'}
{('I', 'P'): '2020-05-06'}
{('I', 'P'): '2020-05-07'}
{('S', 'D'): '2020-05-04'}
{('S', 'D'): '2020-05-05'}
{('S', 'D'): '2020-05-06'}
{('S', 'D'): '2020-05-07'}
{('S', 'I'): '2020-05-04'}
{('S', 'I'): '2020-05-05'}
{('S', 'I'): '2020-05-06'}
{('S', 'I'): '2020-05-07'}
{('S', 'P'): '2020-05-04'}
{('S', 'P'): '2020-05-05'}
{('S', 'P'): '2020-05-06'}
{('S', 'P'): '2020-05-07'} 
我觉得我想要的组合肯定有一个词,但我想不出来


我如何调整我所拥有的,以满足我的需要?

就获得您确切的建议输出而言,这是我目前的建议:

cp = colleague_pairs[:]
#cpKeys == colleague_pairs is unique pairs. All start with a key, basically.
cpKeys = {i[0] for i in colleague_pairs}
#Assign those keys to a date. That date is the day you start using that key.
#startDay = {k:working_days.index(v) for k,v in zip(cpKeys,working_days)}#x is arbitrary. Doesn't mean anything, here, as a name.
startDay = {'C':0,'D':2,'S':1,'I':3}
print(startDay)
#Make a dict of colleague_pair: 
final = {i:'' for i in sorted(colleague_pairs)}

for i in [('C', 'D'),
        ('C', 'I'),
        ('C', 'P'),
        ('C', 'S'),
        ('S', 'D'),
        ('S', 'I'),
        ('S', 'P'),  
        ('D', 'I'),
        ('D', 'P'),
        ('I', 'P'),]:
    #for each cp
    #declare the key
    key = i[0]
    #Get the date to use by connecting the key
    #to the startDay index position for working_days
    dateToUse = working_days[ startDay[key] ]
    final[i] = dateToUse
    startDay[key] += 1
    if startDay[key] >= len(working_days):
        startDay[key] = 0

我这样做是为了得到你提议的准确输出,按照你提议的顺序。这个想法的关键是使用元组的第一个字符作为键,并跟踪每个元组开始的日期。如果同事对是唯一对,那么如果每个开始日期都是不同的一天,那么这应该是正确的。

因为您要按第一个字母分组并从开始算起指定天数,您需要创建一个新的二维数组,如下所示:

同事对=[
[('C','D'),('C','I'),('C','P'),('C','S'),
[('S','D'),('S','I'),('S','P'),
[('D','I'),('D','P')],
[('I','P')]
]
要实现此格式,您可以使用以下词典:


同事对=[('C','D'),('C','I'),('C','P'),('C','S'),('S','D'),('S','I'),('S','P'),('D','I'),('D','P'),('I','P')]
同事对dict={}
对于成对的cp:
key=cp[0]
如果密钥不在同事\u对中\u dict.keys():
同事配对记录[键]=[]
同事对dict[key].append(i)
其他:
同事对dict[key].append(i)
两个\u dim\u同事\u pairs=同事\u pairs\u dict.values()
然后,您可以使用下一个代码来获得所需的输出

工作日=['2020-05-04','2020-05-05','2020-05-06','2020-05-07']
所需的_输出=[]
天数指数=0
对于范围内的i(len(同事对)):
对于范围内的j(len(同事对[i]):
所需的输出。追加({同事对[i][j]:工作日[days\u index]})
天数_指数+=1
如果(天数指数==len(工作天数)):
天数指数=abs(len(工作天数)-len(同事对[i])+1
天数指数=abs(len(工作天数)-len(同事对[i])+1

Oops!很抱歉我肯定回答了一个不同的问题!!很抱歉将编辑。你可以用制作{first collage:startDate}字典的相同想法来做我认为你实际上要求的事情。
cp = colleague_pairs[:]
#cpKeys == colleague_pairs is unique pairs. All start with a key, basically.
cpKeys = {i[0] for i in colleague_pairs}
#Assign those keys to a date. That date is the day you start using that key.
#startDay = {k:working_days.index(v) for k,v in zip(cpKeys,working_days)}#x is arbitrary. Doesn't mean anything, here, as a name.
startDay = {'C':0,'D':2,'S':1,'I':3}
print(startDay)
#Make a dict of colleague_pair: 
final = {i:'' for i in sorted(colleague_pairs)}

for i in [('C', 'D'),
        ('C', 'I'),
        ('C', 'P'),
        ('C', 'S'),
        ('S', 'D'),
        ('S', 'I'),
        ('S', 'P'),  
        ('D', 'I'),
        ('D', 'P'),
        ('I', 'P'),]:
    #for each cp
    #declare the key
    key = i[0]
    #Get the date to use by connecting the key
    #to the startDay index position for working_days
    dateToUse = working_days[ startDay[key] ]
    final[i] = dateToUse
    startDay[key] += 1
    if startDay[key] >= len(working_days):
        startDay[key] = 0