dimanche 27 septembre 2020

How can I check if a certain combination of values exists in a dictionary in a list?

I have a list of dictionary as follows:

knownShows = [
    {'Parsed Name': 'A', 'Parsed Season':'1', 'Override?': 'N', 'NewName': '', 'NewSeason': ''},
    {'Parsed Name': 'B', 'Parsed Season':'1', 'Override?': 'N', 'NewName': '', 'NewSeason': ''},
    {'Parsed Name': 'C', 'Parsed Season':'2', 'Override?': 'N', 'NewName': '', 'NewSeason': ''},
    {'Parsed Name': 'D', 'Parsed Season':'1', 'Override?': 'N', 'NewName': '', 'NewSeason': ''}
]

And I am using a python script to automatically add new dictionaries (show name with parsed season number) based on the episode name (eg "A - s01e02" becomes Parsed Name "A", and Parsed Season "1").

Is there any way to check if a certain combination of key/value pairs already exists in a dictionary in the list?

I was trying to use a modified version of this solution, turning it into

if not (any(d['Parsed Name'] == showName for d in knownShows) and any(d['Parsed Season'] == str(season) for d in knownShows)):

but this has started causing problems when there are multiple seasons of the same show in the list being parsed.

for example, I have an episode with the show/season combination of:

'Parsed Name' = "A"
'Parsed Season' = "2"

Since both: a show called 'A' and a season '2' already exist in the list, it isnt creating a new entry for Season 2 of A.

Is there any way to change the if statement to check for the combination of Show A and Season 2, and only turn False if this specific combination exists inside the same dictionary inside the list?

Aucun commentaire:

Enregistrer un commentaire