Posts: 17
Threads: 4
Joined: Apr 2020
Hi everyone,
I'm trying to transfer a list to a dictionnary and sort a new dict with key, but it's not success.
This is a list I need transfer:
[
{"numero":"20202020","name" : "Durand", "first":"Martin", "notes":[12,15.5,8,13]},
{"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":"20212021","name" :"Bosse", "first":"Mélissa", "notes":[13,19.5,15,8]},
{"numero":"21202120","name" : "Allard", "first":"Chloé", "notes":[11,9.5,2,8]},
{"numero":"20202020","name" : "Durand", "first":"Martin", "notes":[12,15.5,8,13]}
]
And this is a format of dict I need shows:
{"20202020":{"name" : "Durand", "first":"Martin", "notes":[12,15.5,8,13]},
"21212121":{"name" : "Dupond", "first":"Alain", "notes":[11,9.5,5.5,18]},
...
}
So, with this format: key is numero and value is another.
I REALLY NEED YOU HELP!!!! THANK YOU SO MUCH!!!
Posts: 6,798
Threads: 20
Joined: Feb 2020
What have you tried so far?
Posts: 17
Threads: 4
Joined: Apr 2020
Apr-06-2020, 06:33 PM
(This post was last modified: Apr-06-2020, 06:38 PM by cherry_cherry.)
(Apr-06-2020, 06:23 PM)deanhystad Wrote: What have you tried so far? Hi,
Actually, It's part of my topic: I consider the list under condition that if value of numero isn't duplicated, a new dict will be issued. And I tried but I know it's not right and not okay.I just started learning Python so this is totally I can write now. And thank for your reply!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import json
def check(seq):
new_dict = {}
i = 0
j = 1
for i in seq:
for j in seq:
if i[ 'numero' ] = = j[ 'numero' ]:
print ( "Numero en double!" )
print ( "----> " , i[ 'numero' ])
return ( "Fichier non utilisable!" )
else :
a = str (i[ 'name' ])
new_dict.update({i[ "numero" ]: a})
return new_dict
|
Posts: 8,160
Threads: 160
Joined: Sep 2016
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
data = [
{ "numero" : "20202020" , "name" : "Durand" , "first" : "Martin" , "notes" :[ 12 , 15.5 , 8 , 13 ]},
{ "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" : "20212021" , "name" : "Bosse" , "first" : "Mélissa" , "notes" :[ 13 , 19.5 , 15 , 8 ]},
{ "numero" : "21202120" , "name" : "Allard" , "first" : "Chloé" , "notes" :[ 11 , 9.5 , 2 , 8 ]},
{ "numero" : "20202020" , "name" : "Durand" , "first" : "Martin" , "notes" :[ 12 , 15.5 , 8 , 13 ]}
]
new_dict = {}
for element in data:
(key, value), * rest = element.items()
new_dict[value] = dict (rest)
print (new_dict)
|
Output: {'20202020': {'name': 'Durand', 'first': 'Martin', 'notes': [12, 15.5, 8, 13]}, '21212121': {'name': 'Dupond', 'first': 'Alain', 'notes': [11, 9.5, 5.5, 18]}, '21202120': {'name': 'Allard', 'first': 'Chloé', 'notes': [11, 9.5, 2, 8]}, '20212021': {'name': 'Bosse', 'first': 'Mélissa', 'notes': [13, 19.5, 15, 8]}}
Posts: 17
Threads: 4
Joined: Apr 2020
(Apr-06-2020, 06:34 PM)buran Wrote:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
data = [
{ "numero" : "20202020" , "name" : "Durand" , "first" : "Martin" , "notes" :[ 12 , 15.5 , 8 , 13 ]},
{ "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" : "20212021" , "name" : "Bosse" , "first" : "Mélissa" , "notes" :[ 13 , 19.5 , 15 , 8 ]},
{ "numero" : "21202120" , "name" : "Allard" , "first" : "Chloé" , "notes" :[ 11 , 9.5 , 2 , 8 ]},
{ "numero" : "20202020" , "name" : "Durand" , "first" : "Martin" , "notes" :[ 12 , 15.5 , 8 , 13 ]}
]
new_dict = {}
for element in data:
(key, value), * rest = element.items()
new_dict[value] = dict (rest)
print (new_dict)
|
Output: {'20202020': {'name': 'Durand', 'first': 'Martin', 'notes': [12, 15.5, 8, 13]}, '21212121': {'name': 'Dupond', 'first': 'Alain', 'notes': [11, 9.5, 5.5, 18]}, '21202120': {'name': 'Allard', 'first': 'Chloé', 'notes': [11, 9.5, 2, 8]}, '20212021': {'name': 'Bosse', 'first': 'Mélissa', 'notes': [13, 19.5, 15, 8]}}
Thank you so much! And I will remember tag.
|