Python: 如何计算重复项并将嵌套子列表与另一个嵌套子列表进行比较?

Python: How to count duplicates and compare nested sublist with another nested sublist?

我有一个生成随机数据的函数

    def create_Class(self):

        for x in mD.get_module_Code:
            self.module_ID.append(x)

        for j in rM.get_id:
            self.room_ID.append(j)

        for t in tM.get_timeID:
            self.time_ID.append(t)

        for i in gP.get_groupSize:
            self.number_of_Students.append(i)

        for z in mD.get_module_lecturer:
            self.lecturer_ID.append(z)

        # Random Module
        mod = random.choice(range(len(self.module_ID)))
        module = self.module_ID[mod]

        # course
        crs = mD.get_module_Course_ID[mod]

        # Random room
        rom = random.choice(range(len(self.room_ID)))
        room_ID = self.room_ID[rom]

        # random time
        tim = random.choice(range(len(self.time_ID)))
        time_Slot = self.time_ID[tim]

        # lecturer
        lec = self.lecturer_ID[mod]

        self._Class = [[module, crs, lec], [room_ID], [time_Slot]]

        return self._Class

产生单个随机数class

[[5019, 'BSC2', 'ST3'], ['LR1'], ['TTM3']]

然后我创建一个函数 运行 上述代码 15 次(3 classes x 5 天)以创建 1 个嵌套列表来表示时间表。

def create_timetables(self):

        # Random classes
        self.Slots = [self.create_Sessions() for _ in range(self.number_of_classes)]

        return self.Slots

输出:

[[[6224, 'BSC1', 'ST4'], ['LR1'], ['MTM3']], [[4222, 'BSC1', 'ST6'], ['LR1'], ['MTM3']], [[4210, 'BSC1', 'ST1'], ['CR1'], ['TTM2']], [[4210, 'BSC1', 'ST1'], ['CR1'], ['FTM3']], [[5019, 'BSC2', 'ST3'], ['LH1'], ['FTM3']], [[6008, 'BSC3', 'ST1'], ['LB1'], ['WTM1']], [[4201, 'BSC1', 'ST1'], ['LH1'], ['THTM2']], [[4227, 'BSC1', 'ST4'], ['CR1'], ['WTM3']], [[4220, 'BSC2', 'ST5'], ['LH2'], ['THTM2']], [[6226, 'BSC3', 'ST6'], ['CR1'], ['FTM3']], [[6226, 'BSC3', 'ST6'], ['LH1'], ['FTM1']], [[5225, 'BSC2', 'ST6'], ['LB1'], ['THTM3']], [[5201, 'BSC2', 'ST2'], ['LH2'], ['FTM5']], [[4202, 'BSC1', 'ST3'], ['LH1'], ['THTM3']], [[4227, 'BSC1', 'ST4'], ['LH2'], ['THTM2']]]

第一个问题是:如何计算输出中的重复项数。例如[4210,'BSC1','ST1']出现2次,[6226,'BSC3','ST6']出现2次,[4227,'BSC1','ST4']出现2次,以此类推。

第二个问题:如何查看同一时间同一房间是否有不同的class?例如,前两个 class 正在同一时间(MTM3)和同一房间(LR1)举行。每次发生这种情况我都想为冲突+1

我想为时间表创建一个评分系统,所以这就是我所做的。

 def clash_Calculation(self, classes):

        clashes = 0

        for x in classes:

            # if a lecturer is teaching different classes at the same time
            if x[0][0] != x[0][0] and x[0][2] == x[0][2] and x[2] == x[2]:
                clashes += 1

            # if the same group has different class at the same time
            if x[0][0] != x[0][0] and x[0][1] == x[0][1] and x[2] == x[2]:
                clashes += 1

            # if the same group has different class at different same time
            if x[0][0] != x[0][0] and x[0][1] == x[0][1] and x[1] != x[1] and x[2] != x[2]:
                clashes += 1

            # if there is a duplicate class at different room and different times
            if x[0][0] == x[0][0] and x[1] != x[1] and x[2] != x[2]:
                clashes += 1

            # if there is a duplicate class at same room and sane times
            if x[0][0] == x[0][0] and x[1] == x[1] and x[2] == x[2]:
                clashes += 1

            # if there is a duplicate class at same time different room
            if x[0][0] == x[0][0] and x[1] != x[1] and x[2] == x[2]:
                clashes += 1

            # if there is a duplicate class at different time same room
            if x[0][0] == x[0][0] and x[1] == x[1] and x[2] != x[2]:
                clashes += 1

        self.clashes += clashes

        return self.clashes
EDIT: 
[[5019, 'BSC2', 'ST3'], ['LR1'], ['TTM3']]

5019 - Represent the module mode
'BSC2' - Represents the course code
'ST3' - Represents the lecturer ID
'LR1' - Represents room ID
'TTM3' - Represents timeslot ID

These combines into one nested list which represents a single lecture information

此答案是根据您提供的输出给出的:

outputs = [[[6224, 'BSC1', 'ST4'], ['LR1'], ['MTM3']], [[4222, 'BSC1', 'ST6'], ['LR1'], ['MTM3']], [[4210, 'BSC1', 'ST1'], ['CR1'], ['TTM2']], [[4210, 'BSC1', 'ST1'], ['CR1'], ['FTM3']], [[5019, 'BSC2', 'ST3'], ['LH1'], ['FTM3']], [[6008, 'BSC3', 'ST1'], ['LB1'], ['WTM1']], [[4201, 'BSC1', 'ST1'], ['LH1'], ['THTM2']], [[4227, 'BSC1', 'ST4'], ['CR1'], ['WTM3']], [[4220, 'BSC2', 'ST5'], ['LH2'], ['THTM2']], [[6226, 'BSC3', 'ST6'], ['CR1'], ['FTM3']], [[6226, 'BSC3', 'ST6'], ['LH1'], ['FTM1']], [[5225, 'BSC2', 'ST6'], ['LB1'], ['THTM3']], [[5201, 'BSC2', 'ST2'], ['LH2'], ['FTM5']], [[4202, 'BSC1', 'ST3'], ['LH1'], ['THTM3']], [[4227, 'BSC1', 'ST4'], ['LH2'], ['THTM2']]]

问题 #1:第一个问题是:如何计算输出中的重复项数。

根据您的示例,我假设您正在寻找 [module, crs, lec] 个重复项:

# I cast tuple in order to be hashable in a set
module_mapper = map(lambda x: tuple(x[0]), outputs)
# Note: you can change the lists to tuples in your class to avoid the casting

# Sets allow only unique elements
unique_modules = set(module_mapper)

# number of duplicates
duplicate_counter = len(xs) - len(unique_modules)


print(duplicate_counter)  # result: 3

问题#2:检查是否有不同的class同时出现在同一个房间

下面给出了不同的 classes 的列表,它们在同一时间和房间:

# this is our condition
def filter_condition(x, y):
    return x != y and x[1:] == y[1:]


def filterer(classes, acc=[]):
    if classes:
        c, cs = classes[0], classes[1:]
        if c not in acc:
            filtered_classes = list(filter(lambda x: filter_condition(c, x), cs))
            if filtered_classes:
                acc.extend(filtered_classes + [c])
        return filterer(cs, acc)
    else:
        return acc

# results

print(filterer(outputs, []))
# [[[4222, 'BSC1', 'ST6'], ['LR1'], ['MTM3']],
#  [[6224, 'BSC1', 'ST4'], ['LR1'], ['MTM3']],
#  [[6226, 'BSC3', 'ST6'], ['CR1'], ['FTM3']],
#  [[4210, 'BSC1', 'ST1'], ['CR1'], ['FTM3']],
#  [[4227, 'BSC1', 'ST4'], ['LH2'], ['THTM2']],
#  [[4220, 'BSC2', 'ST5'], ['LH2'], ['THTM2']]]

最后注意:如果您使用 python 10.x,那么您可以将 ifs 替换为 match/case 以看起来更干净