Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用条件从Python中的列表中删除随机项_Python_List_Random - Fatal编程技术网

使用条件从Python中的列表中删除随机项

使用条件从Python中的列表中删除随机项,python,list,random,Python,List,Random,我遇到了这种情况: 一系列的工作日(比如轮班),以及不同数量的人,他们无法在特定的工作日轮班。该范围内的每一天必须由两名工人负责 因此,我找到的新手解决方案是,在列表中依次显示每个工人的空闲时间,当他们无法工作时,用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

我遇到了这种情况:

一系列的工作日(比如轮班),以及不同数量的人,他们无法在特定的工作日轮班。该范围内的每一天必须由两名工人负责

因此,我找到的新手解决方案是,在列表中依次显示每个工人的空闲时间,当他们无法工作时,用
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

现在它很有用。考虑到我的工作只是为了学习。现在我想从列表中删除随机项,以便该范围内每天只有两个人。我被困在这里了

解决您的代码使用问题时,您只需在计划中循环并添加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)

想要的输出是什么?嗨,@SufiyanGhori。我想要相同的输出,但是对于第一个索引(第1天),只有两个人在上面。不是三个,像实际输出。这听起来像是一个离散优化问题,我会在计数前在日程表上加入一个random.shuffle,这样轮班就会被随机分配。谢谢