uaveiro-leci/1ano/fp/aula08/names.py

20 lines
536 B
Python
Raw Normal View History

2022-11-18 10:45:54 +00:00
def main():
with open('names.txt', 'r') as f:
names = f.read().split('\n')
surnames = set([name.split(' ')[-1] for name in names[1:]])
namesDict = {}
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)
def printNames(namesDict):
for surname in namesDict:
print(f"{surname} : {namesDict[surname]}")
if __name__ == "__main__":
main()