我遇到了以下问题: 我有N个工人。它们中的每一个都可以分配给4个任务T1,T2,T3和T4中的一个。我想生成一个包含所有赋值可能性的列表:
W1->T1, W2->T1, ... Wx->T1
W1->T1, W2->T1, ... Wx->T2
W1->T1, W2->T2, ... Wx->T2
...
W1->T4, W2->T4, ... Wx->T4
我该怎么做? Python或VB.NET代码是最好的,但任何语言都可以。
答案 0 :(得分:2)
你可以这样做:
import itertools
def show_assignments(workers, tasks):
prod = itertools.product(*[range(tasks)]*workers)
for a in prod:
print(", ".join("W%d->T%d" % (w+1, t+1) for (w, t) in enumerate(a)))
例如,您可以将其称为:
show_assignments(5, 4)
这将显示结果:
W1->T1, W2->T1, W3->T1, W4->T1, W5->T1
W1->T1, W2->T1, W3->T1, W4->T1, W5->T2
...
W1->T4, W2->T4, W3->T4, W4->T4, W5->T3
W1->T4, W2->T4, W3->T4, W4->T4, W5->T4
答案 1 :(得分:0)
您可以使代码更好。但是举一个例子,这将起作用(python 3.6)
workers = ["W1","W2","W3","W4","W5"] #workers
tasks = ["T1","T2","T3","T4"] #tasks
for worker in workers:
for task in tasks:
print(worker + " >> " + task)
答案 2 :(得分:0)