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



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

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

产生单个随机类

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

然后,我创建一个函数来运行上面的代码15次(3个类x 5天(,以创建一个嵌套列表来表示时间表。

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","ST 6"]出现两次,[4227,"BSC2","st 4"]出现二次,依此类推

第二个问题:我该如何检查在同一时间、同一房间是否有不同的班级?例如,前两节课在同一时间(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:检查是否在同一时间和同一房间有不同的班级

以下列出了在同一时间和房间的不同类别:

# 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,那么你可以用match/case代替ifs,看起来更干净

最新更新