Posts: 17
Threads: 4
Joined: Apr 2020
(Apr-08-2020, 03:29 PM)deanhystad Wrote: The condition in your first example does not return a new_dict when else. The else condition adds an entry to new_dict. Printing an error message does nothing to stop processing the list or stop adding items to the dictionary, and it does not return an empty dictionary.
What happens if you replace printing the error message with "return {}"?
I think it return None.
Posts: 6,824
Threads: 20
Joined: Feb 2020
It returns an empty dictionary. None is something different.
Posts: 17
Threads: 4
Joined: Apr 2020
Apr-08-2020, 04:15 PM
(This post was last modified: Apr-08-2020, 04:15 PM by cherry_cherry.)
(Apr-08-2020, 04:07 PM)deanhystad Wrote: It returns an empty dictionary. None is something different.
Now this code is ok but when I use with other function, a new_dict isn't emty. And I did not find a solution to solve the notification problem.
1 2 3 4 5 6 7 8 9 |
def check(seq):
for i in seq:
(key, value), * rest = i.items()
if value not in seq:
new_dict[value] = dict (rest)
else :
return ( "Numero en double! " + "\n---->" +
value + "\nFichier non utilisable!" )
return new_dict
|
Posts: 6,824
Threads: 20
Joined: Feb 2020
Apr-08-2020, 05:01 PM
(This post was last modified: Apr-08-2020, 05:01 PM by deanhystad.)
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?
1 2 3 4 5 6 7 8 9 10 11 12 |
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
|
Posts: 17
Threads: 4
Joined: Apr 2020
(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?
1 2 3 4 5 6 7 8 9 10 11 12 |
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]
Posts: 6,824
Threads: 20
Joined: Feb 2020
Apr-08-2020, 07:45 PM
(This post was last modified: Apr-09-2020, 04:38 AM by deanhystad.)
Was I understanding the problem wrong the entire time? In an early post you said this:
"If If the list has a number of key "numero" duplicated, it does not create a new dict. New dict is only available when all values of key "numero" are unique."
and this:
"yes, it return an empty dic."
I interpreted that as:
If numero is unique for each item in list
return dictionary made from list
else
return empty dictionary
Now it sounds like what you want is:
If numero is unique for each item in list
add items to existing dictionary
To do that you need to test the entire list first and only add entries if the list passes the uniqueness test.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
f = [
{ "numero" : "20202020" , "name" : "Durand" , "first" : "Martin" , "notes" :[ 15 , 15.5 , 8 , 13 ]},
{ "numero" : "21212121" , "name" : "Dupond" , "first" : "Alain" , "notes" :[ 11 , 9.5 , 5.5 , 18 ]},
{ "numero" : "28790020" , "name" : "Férien" , "first" : "Mélissa" , "notes" :[ 13 , 19.5 , 15 , 8 ]},
{ "numero" : "20212021" , "name" : "Bosse" , "first" : "Mélissa" , "notes" :[ 13 , 19.5 , 15 , 8 ]},
{ "numero" : "21202120" , "name" : "Allard" , "first" : "Chloé" , "notes" :[ 11 , 9.5 , 2 , 17 ]},
{ "numero" : "29019022" , "name" : "Durand" , "first" : "Alan" , "notes" :[ 12 , 15.5 , 8 , 13 ]},
]
g = [
{ "numero" : "20202020" , "name" : "Durand" , "first" : "Martin" , "notes" :[ 15 , 15.5 , 8 , 13 ]},
{ "numero" : "21212121" , "name" : "Dupond" , "first" : "Alain" , "notes" :[ 11 , 9.5 , 5.5 , 18 ]},
{ "numero" : "28790020" , "name" : "Férien" , "first" : "Mélissa" , "notes" :[ 13 , 19.5 , 15 , 8 ]},
{ "numero" : "20212021" , "name" : "Bosse" , "first" : "Mélissa" , "notes" :[ 13 , 19.5 , 15 , 8 ]},
{ "numero" : "21202120" , "name" : "Allard" , "first" : "Chloé" , "notes" :[ 11 , 9.5 , 2 , 17 ]},
{ "numero" : "29019022" , "name" : "Durand" , "first" : "Alan" , "notes" :[ 12 , 15.5 , 8 , 13 ]},
{ "numero" : "21212121" , "name" : "Dupond" , "first" : "Alain" , "notes" :[ 11 , 9.5 , 5.5 , 18 ]},
]
new_dict = {}
def add(seq):
for i in range ( len (seq) - 1 ):
for j in range (i + 1 , len (seq)):
if seq[i][ 'numero' ] = = seq[j][ 'numero' ]:
print ( "Numero en double!\n " + "----> " + seq[i][ 'numero' ] + " \n Fichier non utilisable!" )
return new_dict
for i in seq:
(key, value), * rest = i.items()
new_dict[value] = dict (rest)
return new_dict
print (add(f))
print (add(g))
|
Posts: 17
Threads: 4
Joined: Apr 2020
(Apr-08-2020, 07:45 PM)deanhystad Wrote: Was I understanding the problem wrong the entire time? In an early post you said this:
"If If the list has a number of key "numero" duplicated, it does not create a new dict. New dict is only available when all values of key "numero" are unique."
and this:
"yes, it return an empty dic."
I interpreted that as:
If numero is unique for each item in list
return dictionary made from list
else
return empty dictionary
Now it sounds like what you want is:
If numero is unique for each item in list
add items to existing dictionary
To do that you need to test the entire list first and only add entries if the list passes the uniqueness test.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
f = [
{ "numero" : "20202020" , "name" : "Durand" , "first" : "Martin" , "notes" :[ 15 , 15.5 , 8 , 13 ]},
{ "numero" : "21212121" , "name" : "Dupond" , "first" : "Alain" , "notes" :[ 11 , 9.5 , 5.5 , 18 ]},
{ "numero" : "28790020" , "name" : "Férien" , "first" : "Mélissa" , "notes" :[ 13 , 19.5 , 15 , 8 ]},
{ "numero" : "20212021" , "name" : "Bosse" , "first" : "Mélissa" , "notes" :[ 13 , 19.5 , 15 , 8 ]},
{ "numero" : "21202120" , "name" : "Allard" , "first" : "Chloé" , "notes" :[ 11 , 9.5 , 2 , 17 ]},
{ "numero" : "29019022" , "name" : "Durand" , "first" : "Alan" , "notes" :[ 12 , 15.5 , 8 , 13 ]},
]
g = [
{ "numero" : "20202020" , "name" : "Durand" , "first" : "Martin" , "notes" :[ 15 , 15.5 , 8 , 13 ]},
{ "numero" : "21212121" , "name" : "Dupond" , "first" : "Alain" , "notes" :[ 11 , 9.5 , 5.5 , 18 ]},
{ "numero" : "28790020" , "name" : "Férien" , "first" : "Mélissa" , "notes" :[ 13 , 19.5 , 15 , 8 ]},
{ "numero" : "20212021" , "name" : "Bosse" , "first" : "Mélissa" , "notes" :[ 13 , 19.5 , 15 , 8 ]},
{ "numero" : "21202120" , "name" : "Allard" , "first" : "Chloé" , "notes" :[ 11 , 9.5 , 2 , 17 ]},
{ "numero" : "29019022" , "name" : "Durand" , "first" : "Alan" , "notes" :[ 12 , 15.5 , 8 , 13 ]},
{ "numero" : "21212121" , "name" : "Dupond" , "first" : "Alain" , "notes" :[ 11 , 9.5 , 5.5 , 18 ]},
]
new_dict = {}
def add(seq):
for i in range ( len (seq) - 1 ):
for j in range (i + 1 , len (seq)):
if seq[i][ 'numero' ] = = seq[j][ 'numero' ]:
print ( "Numero en double!\n " + "----> " + seq[i][ 'numero' ] + " \n Fichier non utilisable!" )
return new_dict
for i in seq:
(key, value), * rest = i.items()
new_dict[value] = dict (rest)
return new_dict
print (add(f))
print (add(g))
|
Thank you so much!
|