【问题标题】:Distribute values over set of tuples in Python在 Python 中在一组元组上分配值
【发布时间】:2020-04-30 01:25:53
【问题描述】:

由于锁定,我正在尝试为配对对话创建列表。

我有一份同事名单。

我已经创建了一个日期列表。

我想为每一对按顺序分配日期,直到所有对都分配完毕。

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

这是我所拥有的:

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

我想最终将所有日期应用于所有唯一对:

{('C', 'D'): '2020-05-04'}
{('C', 'I'): '2020-05-05'}
{('C', 'P'): '2020-05-06'}
{('C', 'S'): '2020-05-07'}

{('S', 'D'): '2020-05-05'}
{('S', 'I'): '2020-05-06'}
{('S', 'P'): '2020-05-07'}

{('D', 'I'): '2020-05-06'}
{('D', 'P'): '2020-05-07'}

{('I', 'P'): '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'} 

我觉得我想要的组合必须有一个词,但我想不出来。

我怎样才能将我所拥有的东西调整到我需要的东西?

【问题讨论】:

    标签: python combinations


    【解决方案1】:

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

    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
    

    我这样做是为了按照您提出的顺序获得您提出的确切输出。这个想法的关键是使用元组的第一个字符作为键,并跟踪每个字符开始的日期。如果 colleague_pairs 是唯一的对,那么如果每个 startDay 是不同的一天,这应该会成功。

    【讨论】:

    • 糟糕!对不起!我肯定会回答一个不同的问题!对不起!会编辑。您可以使用制作 {first colleage:startDate} 字典的相同想法来执行我认为您实际要求的操作。
    【解决方案2】:

    由于您想按首字母分组并指定从开始的天数,您需要创建一个新的二维数组,如下所示:

    colleague_pairs = [
        [('C', 'D'), ('C', 'I'), ('C', 'P'), ('C', 'S')],
        [('S', 'D'), ('S', 'I'), ('S', 'P')],
        [('D', 'I'), ('D', 'P')],
        [('I', 'P')]
    ]
    

    要实现这种格式,您可以使用如下字典:

    
    colleague_pairs = [('C', 'D'), ('C', 'I'), ('C', 'P'), ('C', 'S'), ('S', 'D'), ('S', 'I'), ('S', 'P'), ('D', 'I'), ('D', 'P'), ('I', 'P')]
    colleague_pairs_dict = {}
    for cp in colleague_pairs:
        key = cp[0]
        if key not in colleague_pairs_dict.keys():
            colleague_pairs_dict[key] = []
            colleague_pairs_dict[key].append(i)
        else:
            colleague_pairs_dict[key].append(i)
    
    two_dim_colleague_pairs = colleague_pairs_dict.values()
    

    然后,您可以使用下一个代码来获得所需的输出。

    working_days = ['2020-05-04', '2020-05-05', '2020-05-06', '2020-05-07']
    desired_output = []
    days_index = 0
    
    for i in range(len(colleague_pairs)):
        for j in range(len(colleague_pairs[i])):
            desired_output.append( { colleague_pairs[i][j] : working_days[days_index] } )
            days_index += 1
            if(days_index == len(working_days)):
                days_index = abs(len(working_days) - len(colleague_pairs[i])) + 1
        days_index = abs(len(working_days) - len(colleague_pairs[i])) + 1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-15
      • 2020-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-16
      • 2017-11-03
      相关资源
      最近更新 更多