我有这种情况:
几天的时间范围(比如说轮班),而在特定的日子里,无法覆盖轮班的人数有所不同。该范围的每一天都必须由两名工人负责。
因此,我发现的新手解决方案是,在列表中依次显示每个工作人员有空的工作日,并在他们无法工作时打上0
标记。
period_of_time = range(1,10)
human_1 = [1, 3, 4, 8, "Human 1"]
human_2 = [5, 6, "Human 2"]
human_3 = [8, 9, "Human 3"]
human_4 = [2, 4, 6, "Human 4"]
humans = [human_1, human_2, human_3, human_4]
def looping_function(in_humans):
new = []
for d in period_of_time:
if d not in in_humans:
new.append(d)
else:
new.append(0)
print(str(new) + " " + human_id + "\n")
for a in humans:
in_humans = a
human_id = a[-1]
looping_function(in_humans)
很好。
[0, 2, 0, 0, 5, 6, 7, 0, 9] Human 1
[1, 2, 3, 4, 0, 0, 7, 8, 9] Human 2
[1, 2, 3, 4, 5, 6, 7, 0, 0] Human 3
[1, 0, 3, 0, 5, 0, 7, 8, 9] Human 4
目前非常有用。考虑到我只是出于学习目的而工作。现在,我想从列表中删除随机项目,以便该范围的每一天只有两个人。我被困在这里。
答案 0 :(得分:1)
使用代码的解决方案,您只需要遍历计划并添加ID
period_of_time = range(1,10)
human_1 = [1, 3, 4, 8, "Human 1"]
human_2 = [5, 6, "Human 2"]
human_3 = [8, 9, "Human 3"]
human_4 = [2, 4, 6, "Human 4"]
humans = [human_1, human_2, human_3, human_4]
def looping_function(in_humans):
new = []
for d in period_of_time:
if d not in in_humans:
new.append(d)
else:
new.append(0)
print(str(new) + " " + human_id + "\n")
return new
schedule = []
for a in humans:
in_humans = a
human_id = a[-1]
schedule.append(looping_function(in_humans))
for x in range(9):
current_day_workers = 0
for human in schedule:
if human[x] != 0: current_day_workers +=1
if current_day_workers >2: human[x] = 0
print(schedule)