简体   繁体   English

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

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

I have a function that generates a random data我有一个生成随机数据的 function

    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

Which produce a single random class其中单随机产生class

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

I then create a function to run the code above 15 times (3 classes x 5 days) to create 1 nested list to represent a timetable.然后我创建一个 function 来运行上面的代码 15 次(3 节课 x 5 天)以创建 1 个嵌套列表来表示时间表。

def create_timetables(self):

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

        return self.Slots

output: output:

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

1st question is: How do I count the number of duplicates in the output. For example, [4210, 'BSC1', 'ST1'] appears 2 times and [6226, 'BSC3', 'ST6'] 2 times, [4227, 'BSC1', 'ST4'] appears 2 times and so on.第一个问题是:如何计算output中的重复次数。例如,[4210,'BSC1','ST1']出现2次,[6226,'BSC3','ST6']出现2次,[4227 , 'BSC1', 'ST4'] 出现2次等等。

2nd question: How do I check if there is a different class at the same time and at the same room?第二个问题:如何查看同一时间同一房间是否有不同的class? For example, the first two class are being held at the same time (MTM3) and same room (LR1).例如,前两个 class 在同一时间(MTM3)和同一房间(LR1)举行。 I would like to +1 to the clashes every time this happens每次发生这种情况时,我都想为冲突+1

I want to create a scoring system for the timetable and so this is what I did.我想为时间表创建一个评分系统,所以这就是我所做的。

 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

This answer is given according to the output you've provided:这个答案是根据你提供的output给出的:

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

Question #1: 1st question is: How do I count the number of duplicates in the output.问题 #1:第一个问题是:如何计算 output 中的重复项数。

According to your examples, I assume you're looking for [module, crs, lec] duplicates:根据您的示例,我假设您正在寻找[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

Question #2: Check if there is a different class at the same time and at the same room问题#2:检查同一时间和同一房间是否有不同的 class

The following is giving a list of different classes which are at the same time and room:以下是同时在同一时间和房间的不同班级的列表:

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

Final Note: If you use python 10.x , then you can replace ifs with match/case to look cleaner最后注意:如果你使用python 10.x ,那么你可以用match/case替换ifs看起来更干净

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM