lundi 29 juin 2020

Compound if condition inside list comprehension doesn't seem to work

I have this code:

 jira_regex = re.compile("^[A-Z][A-Z0-9]+-[0-9]+")
 with open(ticket_file, 'r') as f:
     tickets = [word for line in f for word in line.split() if jira_regex.match(word) and word not in tickets]

ticket_file contains this:

PRJ1-2333
PRJ1-2333
PRJ1-2333
PRJ2-2333
PRJ2-2333
MISC-5002

After the code runs, the tickets list contains these:

['PRJ1-2333', 'PRJ1-2333', 'PRJ1-2333', 'PRJ2-2333', 'PRJ2-2333', 'MISC-5002']

I expected this:

['PRJ1-2333', 'PRJ2-2333', 'MISC-5002']

Why is word not in tickets condition not eliminating duplicates? The regex filter is working fine, however.

Aucun commentaire:

Enregistrer un commentaire