Skip to content Skip to sidebar Skip to footer

How To Compare Two Lists By Filtering And Sorting "repeated" Values

I have the following act2.txt file for an email campaign: 2021-04-02//email@example.com//Enhance your presentation skills in 15 minutes//Open 2021-04-11//email@example.com//Enroll

Solution 1:

You need to think of this in a different way, you are not combining lists.

If an email was opened, that means it was also received. This means that your opened list is also your combined list.

After you realize that, all you have to do is copy the unopened emails to a result list for emails that ere not opened.

Go over the opened emails list and copy the subjects into a set, after that go over the received emails and check if the subject is in the set, if it is then do nothing. If the subject isn't in the set then copy it to unopened emails list.

It is a very simple piece of code:

opened_subjects = set()
unopened = []
for email in opened:
    opened_subjects.add(email[2])

unopened_subjects = set()
for email in received:
    ifall(email[2] notin subj_set 
           for subj_set in (opened_subjects, unopened_subjects)):
        unopened.append(email)
        unopened_subjects.add(email[2])

print('Both received and opened:', opened)
print('Unopened emails:', unopened)

A small note - The reason for the each of the sets is different. The first set opened_subjects is there because of the set's ability to contain only unique items, and that is what is required in this case. The second set unopened_subjects is there because it is faster to check if an item is in a set than in a list, seeing as I am checking before adding to the set any way then there is no requirement for the set ability to store unique only.

Post a Comment for "How To Compare Two Lists By Filtering And Sorting "repeated" Values"