Apr-08-2020, 06:03 PM
(Apr-08-2020, 05:01 PM)deanhystad Wrote: Why did you change the code to create new_dict outside of check()? Why do you insist on not returning an empty diction while insisting that returning an empty dictionary is what the code should do when it finds a duplicate name?
def check(seq): new_dict = {} for i in seq: for j in seq: if (j!=i and (i['numero'] == j['numero'])): print('Dupicate, I['numero']) return {} else: (key, value), *rest = i.items() new_dict[value] = dict(rest) return new_dict
Due to the fact that when executing the program with another function, new_dict is not empty.
This's my output when I test:
Output:Donner le nom du fichier avec l'extension ['.json','.txt']:
source1bad1.json
Voici le fichier choisi: source1bad1.json
Donner le nom du fichier avec l'extension ['.json','.txt']:
source1bad1.json
Données source:
[{'numero': '21212121', 'name': 'Dupond', 'first': 'Alain', 'notes': [11, 9.5, 5.5, 18]}, {'numero': '21202120', 'name': 'Bru', 'first': 'Mélissa', 'notes': [11, 19.5, 15, 8]}, {'numero': '20212023', 'name': 'Bosse', 'first': 'Mélissa', 'notes': [13, 19.5, 15, 8]}, {'numero': '20212023', 'name': 'Bosse', 'first': 'Mélissa', 'notes': [13, 19.5, 15, 8]}]
Dictionnaire extrait:
21212121 - Dupond , Alain , [11, 9.5, 5.5, 18]
21202120 - Bru , Mélissa , [11, 19.5, 15, 8]
20212023 - Bosse , Mélissa , [13, 19.5, 15, 8]