I'm trying to find a way to make a dictionary out of multiple items in lists using a Python script. The lists in question look like this, just to name a 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 is where I'm finding trouble because I'm not sure how to go about doing this. The first item is an ID, the second item an organism genus name, and sometimes there is a species name given as the third item and sometimes there is not, as is the case for the second list. I need to create a dictionary using the ID number as the key and the organism genus and species name (if given) as the value.
How might I go about doing this? I am currently 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 = {}
for 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'}
}