python - Making a dictionary from multiple items in lists -


i'm trying find way make dictionary out of multiple items in lists using python script. lists in question this, name few:

['331416', 'macromedaeus', 'distinguendus', '|', '|', 'scientific', 'name','|'] ['331417', 'physalopteroidea', '|', '|', 'scientific', 'name', '|'] ['331418', 'dracunculus', 'insignis', '|', '|', 'scientific', 'name', '|'] ['331419', 'bejaria', 'sprucei', '|', '|', 'scientific', 'name', '|'] ['331420', 'paecilomyces', 'sp.', 'jcm', '12545', '|', '|', 'scientific', 'name', '|'] 

this i'm finding trouble because i'm not sure how go doing this. first item id, second item organism genus name, , there species name given third item , there not, case second list. need create dictionary using id number key , organism genus , species name (if given) value.

how might go doing this? using python. 2.7.8.

input = [ ['331416', 'macromedaeus', 'distinguendus', '|', '|', 'scientific', 'name','|'], ['331417', 'physalopteroidea', '|', '|', 'scientific', 'name', '|'], ['331418', 'dracunculus', 'insignis', '|', '|', 'scientific', 'name', '|'], ['331419', 'bejaria', 'sprucei', '|', '|', 'scientific', 'name', '|'], ['331420', 'paecilomyces', 'sp.', 'jcm', '12545', '|', '|', 'scientific', 'name', '|'] ]  taxonomy = {} r in input:   taxonomy[r[0]] = {}   taxonomy[r[0]]['genus'] = r[1]   if r[2] != '|':     taxonomy[r[0]]['specie'] = " ".join(r[2:r.index("|")]) 

get following output in taxonomy

{ '331418': {'genus': 'dracunculus', 'specie': 'insignis'},  '331419': {'genus': 'bejaria', 'specie': 'sprucei'},  '331420': {'genus': 'paecilomyces', 'specie': 'sp. jcm 12545'},  '331416': {'genus': 'macromedaeus', 'specie': 'distinguendus'},  '331417': {'genus': 'physalopteroidea'} } 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -