Converted names.py to a list comprehension

This commit is contained in:
TiagoRG 2022-11-28 18:01:03 +00:00
parent e8319244b3
commit 1c2f80684e
Signed by untrusted user who does not match committer: TiagoRG
GPG Key ID: DFCD48E3F420DB42
1 changed files with 2 additions and 5 deletions

View File

@ -1,12 +1,9 @@
def main(): def main():
with open('names.txt', 'r') as f: with open('names.txt', 'r') as f:
names = f.read().split('\n') names = f.read().split('\n')
surnames = set([name.split(' ')[-1] for name in names[1:]]) namesDict = {surname: set([name.split(' ')[0] for name in names[1:] if surname in name])
namesDict = {} for surname in set([name.split(' ')[-1] for name in names[1:] if name != ''])}
for surname in surnames:
namesDict[surname] = set([name.split(' ')[0] for name in names[1:] if surname in name and surname != ''])
del namesDict['']
printNames(namesDict) printNames(namesDict)