nrmcdate is a list in an order of name, class, and date. This list represents the class the student did not attend and the date which the student did not attend. eg (john, 5537, datetime.datetime(2021, 5, 15, 0, 0)) means john did not attend class 5537 at 15-5-2021
registered_lists is the list of student that have registered to that class. So in this list there will be repeating names. eg. ('JOHN', 5537), ('JOHN', 5620) means john has registered to class 5620 and 5537.
nrmcdate = [
('JOHN', 5537, datetime.datetime(2021, 5, 15, 0, 0)),
('JOHN', 5620, datetime.datetime(2021, 5, 15, 0, 0)),
('["KSXXGPSPZQWTS\'SVZCIXZOOZQGO"]', 5537, datetime.datetime(2021, 8, 3, 0, 0)),
('["KSXXGPSPZQWTS\'SVZCIXZOOZQGO"]', 5537, datetime.datetime(2021, 8, 10, 0, 0)),
('["KSXXGPSPZQWTS\'SVZCIXZOOZQGO"]', 5558, datetime.datetime(2021, 8, 12, 0, 0)),
('["KSXXGPSPZQWTS\'SVZCIXZOOZQGO"]', 5563, datetime.datetime(2021, 8, 12, 0, 0)),
('["KSXXGPSPZQWTS\'SVZCIXZOOZQGO"]', 5628, datetime.datetime(2021, 5, 20, 0, 0)),
('["KSXXGPSPZQWTS\'SVZCIXZOOZQGO"]', 5628, datetime.datetime(2021, 7, 8, 0, 0)),
("['ASAZPGXWZKSWKZBASJQZCI']", 5531, datetime.datetime(2021, 5, 7, 0, 0)),
("['ASAZPGXWZKSWKZBASJQZCI']", 5531, datetime.datetime(2021, 5, 14, 0, 0)),
("['KSPKGRMQSNSO']", 5558, datetime.datetime(2021, 5, 11, 0, 0)),
("['KSPKGRMQSNSO']", 5558, datetime.datetime(2021, 6, 3, 0, 0)),
("['KSPKGRMQSNSO']", 5558, datetime.datetime(2021, 7, 8, 0, 0)),
("['KSPKGRMQSNSO']", 5558, datetime.datetime(2021, 7, 15, 0, 0)),
("['KSPKGRMQSNSO']", 5558, datetime.datetime(2021, 7, 30, 0, 0)),
("['NSPZOPGXWZTEWQZPSHZKSJIZK']", 5530, datetime.datetime(2021, 5, 7, 0, 0)),
("['NSPZOPGXWZTEWQZPSHZKSJIZK']", 5530, datetime.datetime(2021, 7, 9, 0, 0)),
("['NSPZOPGXWZTEWQZPSHZKSJIZK']", 5530, datetime.datetime(2021, 7, 16, 0, 0)),
("['NSPZOPGXWZTEWQZPSHZKSJIZK']", 5530, datetime.datetime(2021, 8, 6, 0, 0)),
("['NSPZOPGXWZTEWQZPSHZKSJIZK']", 5540, datetime.datetime(2021, 4, 28, 0, 0)),
("['NSPZOPGXWZTEWQZPSHZKSJIZK']", 5540, datetime.datetime(2021, 8, 11, 0, 0)),
("['NSPZOPGXWZTEWQZPSHZKSJIZK']", 5541, datetime.datetime(2021, 4, 27, 0, 0)),
("['NSPZOPGXWZTEWQZPSHZKSJIZK']", 5541, datetime.datetime(2021, 5, 7, 0, 0)),
("['NSPZOPGXWZTEWQZPSHZKSJIZK']", 5541, datetime.datetime(2021, 5, 28, 0, 0)),
("['NSPZOPGXWZTEWQZPSHZKSJIZK']", 5546, datetime.datetime(2021, 5, 6, 0, 0)),
("['NZAZJPGXWZTEKSWKZOSZKZAJZV']", 5531, datetime.datetime(2021, 5, 14, 0, 0)),
("['NZAZJPGXWZTEKSWKZOSZKZAJZV']", 5532, datetime.datetime(2021, 6, 3, 0, 0)),
("['NZAZJPGXWZTEKSWKZOSZKZAJZV']", 5532, datetime.datetime(2021, 7, 15, 0, 0)),
("['NZAZJPGXWZTEKSWKZOSZKZAJZV']", 5535, datetime.datetime(2021, 5, 21, 0, 0)),
("['NZAZJPGXWZTEKSWKZOSZKZAJZV']", 5556, datetime.datetime(2021, 7, 13, 0, 0)),
("['NZAZJPGXWZTEKSWKZOSZKZAJZV']", 5561, datetime.datetime(2021, 5, 11, 0, 0)),
("['NZAZJPGXWZTEKSWKZOSZKZAJZV']", 5561, datetime.datetime(2021, 6, 29, 0, 0)),
("['NZAZJPGXWZTEKSWKZOSZKZAJZV']", 5561, datetime.datetime(2021, 7, 13, 0, 0)),
("['NZAZJPGXWZTEKSWKZOSZKZAJZV']", 5620, datetime.datetime(2021, 7, 30, 0, 0)),
("['NZAZJPGXWZTEKSWKZOSZKZAJZV']", 5626, datetime.datetime(2021, 7, 15, 0, 0)),
("['NZAZJPGXWZTEKSWKZOSZKZAJZV']", 5643, datetime.datetime(2021, 6, 29, 0, 0)),
("['NZAZJPGXWZTEKSWKZOSZKZAJZV']", 5643, datetime.datetime(2021, 8, 10, 0, 0)),
# Many more rows...
]
and
registered_list = [
('JOHN', 5537),
('JOHN', 5620),
('["KSXXGPSPZQWTS\'SVZCIXZOOZQGO"]', 5531),
('["KSXXGPSPZQWTS\'SVZCIXZOOZQGO"]', 5534),
('["KSXXGPSPZQWTS\'SVZCIXZOOZQGO"]', 5537),
('["KSXXGPSPZQWTS\'SVZCIXZOOZQGO"]', 5554),
('["KSXXGPSPZQWTS\'SVZCIXZOOZQGO"]', 5558),
('["KSXXGPSPZQWTS\'SVZCIXZOOZQGO"]', 5563),
('["KSXXGPSPZQWTS\'SVZCIXZOOZQGO"]', 5620),
('["KSXXGPSPZQWTS\'SVZCIXZOOZQGO"]', 5624),
('["KSXXGPSPZQWTS\'SVZCIXZOOZQGO"]', 5628),
('["KSXXGPSPZQWTS\'SVZCIXZOOZQGO"]', 5631),
('["KSXXGPSPZQWTS\'SVZCIXZOOZQGO"]', 5632),
('["KSXXGPSPZQWTS\'SVZCIXZOOZQGO"]', 5635),
('["KZGD\'GPUKJTQEZTQ"]', 5618),
("['ASAZPGXWZKSWKZBASJQZCI']", 5531),
("['ASAZPGXWZKSWKZBASJQZCI']", 5533),
("['ASAZPGXWZKSWKZBASJQZCI']", 5536),
("['ASAZPGXWZKSWKZBASJQZCI']", 5542),
("['ASAZPGXWZKSWKZBASJQZCI']", 5544),
("['ASAZPGXWZKSWKZBASJQZCI']", 5548),
("['ASAZPGXWZKSWKZBASJQZCI']", 5554),
("['ASAZPGXWZKSWKZBASJQZCI']", 5557),
("['ASAZPGXWZKSWKZBASJQZCI']", 5562),
("['ASAZPGXWZKSWKZBASJQZCI']", 5620),
("['ASAZPGXWZKSWKZBASJQZCI']", 5623),
("['ASAZPGXWZKSWKZBASJQZCI']", 5627),
("['BZAZJGQEKSO']", 5643),
("['BZAZJGQEKSO']", 5645),
("['BZAZJGQEKSO']", 5647),
("['CEISJQY,UKSCKZSHUK']", 5530),
("['CEISJQY,UKSCKZSHUK']", 5554),
("['CEISJQY,UKSCKZSHUK']", 5559),
("['CEISJQY,UKSCKZSHUK']", 5564),
("['CEISJQY,UKSCKZSHUK']", 5598),
("['CEISJQY,UKSCKZSHUK']", 5599),
("['CEISJQY,UKSCKZSHUK']", 5642),
("['CZJ,SGJSZNQMI']", 5531),
("['CZJ,SGJSZNQMI']", 5533),
("['CZJ,SGJSZNQMI']", 5536),
("['CZJ,SGJSZNQMI']", 5542),
("['CZJ,SGJSZNQMI']", 5544),
("['CZJ,SGJSZNQMI']", 5548),
("['CZJ,SGJSZNQMI']", 5554),
("['CZJ,SGJSZNQMI']", 5557),
("['CZJ,SGJSZNQMI']", 5562),
("['CZJ,SGJSZNQMI']", 5620),
("['CZJ,SGJSZNQMI']", 5623),
("['CZJ,SGJSZNQMI']", 5627),
("['CZOJTQIKZE']", 5543),
("['CZOJTQIKZE']", 5547),
("['CZOJTQIKZE']", 5551),
("['CZOJTQIKZE']", 5555),
("['UKZSDSHQMU']", 5626),
("['UKZSDSHQMU']", 5643),
("['UKZSDSHQMU']", 5644),
("['UKZSDSHQMU']", 5646),
("['VOPZOZRM/IOSIZVXTOZQMO']", 5538),
("['VOPZOZRM/IOSIZVXTOZQMO']", 5540),
("['VOPZOZRM/IOSIZVXTOZQMO']", 5541),
("['VOPZOZRM/IOSIZVXTOZQMO']", 5620),
("['VOPZOZRM/IOSIZVXTOZQMO']", 5624),
("['VOPZOZRM/IOSIZVXTOZQMO']", 5626),
("['VOPZOZRM/IOSIZVXTOZQMO']", 5631),
("['VOPZOZRM/IOSIZVXTOZQMO']", 5633),
("['VOPZOZRM/IOSIZVXTOZQMO']", 5636),
("['VVSEZPXWZKSWSJESVXZOOZQGO']", 5542),
("['VVSEZPXWZKSWSJESVXZOOZQGO']", 5544),
("['VVSEZPXWZKSWSJESVXZOOZQGO']", 5548),
("['VVSEZPXWZKSWSJESVXZOOZQGO']", 5555),
("['VVSEZPXWZKSWSJESVXZOOZQGO']", 5557),
("['ZSJZNZAXQMOWJSZQGAXZOOZQGO']", 5627)
]
Currently I am required to compare the dates of nrmcdate to itself and see if the dates are 3 days apart. eg.(not from the list above) [(Aang, 12, datetime.datetime(2021, 05, 15)),( Ben, 22, datetime.datetime(2021, 05, 12)] since the objects in the list is three days apart it will then take the name and compare to the registerd_lists. Once the two name have a match in the registered list, the programme will then check to see if Aang and Ben have a common class. output eg. (Aang, Ben , 16) this means Aang and Ben is in the same class 16.
I have been trying to figure out how to compare the dates for the past few weeks and i hope someone here could give a solution.
CodePudding user response:
I would start by listing all the pairs
itertools.combinations(nrmcdate, 2) # don't forget to import
Then for all pairs
def common_class(pair, registered):
person1 = pair[0]
person2 = pair[1]
if (abs((person1[2] - person2[2]).days) >= 3): #check if 3 days apart
if common_classes := set(registered[person1[0]]).intersection(registered[person2[0]]): # they have a common class
return (person1[0], person2[0], common_classes)
return None
at last convert registered_list to a dict using:
result_dict = {}
for x in registered_list:
result_dict.setdefault(x[0], []).append(x[1])
then to put this all toghether:
import datetime
import itertools
nrmcdate = [("Aang", 12, datetime.datetime(2021, 5, 15)),( "Ben", 22, datetime.datetime(2021, 5, 12))]
registered_list = [('Ben', 5537), ('Aang', 5537)]
itertools.combinations(nrmcdate, 2)
def common_class(pair, registered):
person1 = pair[0]
person2 = pair[1]
if (abs((person1[2] - person2[2]).days) >= 3): #check if 3 days apart
if common_classes := set(registered[person1[0]]).intersection(registered[person2[0]]): # they have a common class
return (person1[0], person2[0], common_classes)
return None
result_dict = {}
for x in registered_list:
result_dict.setdefault(x[0], []).append(x[1])
print([res for x in itertools.combinations(nrmcdate, 2) if (res := common_class(x, result_dict))]) # we at last check if the value is not none
I hope this works, it isn't really tested. For example if a name is in nrmcdate but not in registered_list, it would crash but it's not that hard to fix.
