Create dictionary from a list

1

I have this list:

laboratorio= [ [’pol’, (’hematies’, 4430000), (’basofils’, 0.5), (’calci’,
9)],
[’josep’, (’hematies’, 5130000), (’hematocrit’, 40)],
[’enrica’, (’hematies’, 4800000), (’calci’, 11.2), (’colesterol’, 2.3)],
[’paco’, (’calci’, 10.6), (’glucosa’, 0.9)],
[’lidia’, (’hematies’, 4620000), (’hematocrit’, 50), (’basofils’, 0.7)],
[’pau’, (’calci’, 10.1), (’glucosa’, 1.5), (’colesterol’, 2.5)] ]

From this list I need to create a dictionary that the key is the name of the test and the value a list, that is, from the list above I have to create a dictionary that fits me like this:

{’colesterol’: [2.3, 2.5], ’hematocrit’: [40.0, 50.0],
’calci’: [9, 11.2, 10.6, 10.1],
’hematies’: [4430000.0, 5130000.0, 4800000.0, 4620000.0],
’glucosa’: [0.9, 1.5], ’basofils’: [0.5, 0.7]}

I made this loop:

for i in laboratori:
    x=i[1:3]

What results in this:

How do I get the dictionary to stay the way they ask me?

    
asked by Marc 03.06.2018 в 23:51
source

2 answers

2

I'll give you another solution

# declaración de diccionario
resultado = {}
for pruebas in laboratorio:
    for prueba in pruebas:
        # solo nos interesa aquellos datos de la lista que son tuplas
        if isinstance(prueba, tuple):
            # si en el diccionario resultante no tengo esa clave, la inicializó
            if prueba[0] not in resultado:
                resultado[prueba[0]] = []
            # agrego el valor a la clave del diccionario
            # para prueba = (’hematies’, 4430000)
            # clave: prueba[0] => 'hematies'
            # valor: prueba[1] => 4430000
            resultado[prueba[0]].append(prueba[1])

As a result you would get:

print(resultado)
{'colesterol': [2.3, 2.5], 'hematocrit': [40, 50], 'calci': [9, 11.2, 10.6, 10.1], 'hematies': [4430000, 5130000, 4800000, 4620000], 'glucosa': [0.9, 1.5], 'basofils': [0.5, 0.7]}
    
answered by 04.06.2018 / 00:58
source
2

You can use collections.defaultdict to create a dictionary in which values by defect is a list. The advantage is that when you try to access the list associated with a key that does not exist, a new pair is created with the new key and the default value (a list) automatically instead of throwing an exception.

To eliminate the first element you can use slicing as you do or use itertools.islice that does not create a new list as it does the slicing.

import collections
import itertools



laboratorio= [['pol', ('hematies', 4430000), ('basofils', 0.5), ('calci', 9)],
              ['josep', ('hematies', 5130000), ('hematocrit', 40)],
              ['enrica', ('hematies', 4800000), ('calci', 11.2), ('colesterol', 2.3)],
              ['paco', ('calci', 10.6), ('glucosa', 0.9)],
              ['lidia', ('hematies', 4620000), ('hematocrit', 50), ('basofils', 0.7)],
              ['pau', ('calci', 10.1), ('glucosa', 1.5), ('colesterol', 2.5)]
             ]

diccionario = collections.defaultdict(list)

for sublist in laboratorio:
    for key, value in itertools.islice(sublist, 1, None):
        diccionario[key].append(value)

The output is:

>>> diccionario

defaultdict(<class 'list'>, {'hematies': [4430000, 5130000, 4800000, 4620000],
                             'basofils': [0.5, 0.7],
                             'calci': [9, 11.2, 10.6, 10.1],
                             'hematocrit': [40, 50],
                             'colesterol': [2.3, 2.5],
                             'glucosa': [0.9, 1.5]})

defaultdict is a subclass of dict and has all its methods therefore. You can use it like any dictionary. If for any reason you want a dictionary as such, just do:

>>> dict(diccionario)

{'hematies': [4430000, 5130000, 4800000, 4620000],
 'basofils': [0.5, 0.7]
 'calci': [9, 11.2, 10.6, 10.1],
 'hematocrit': [40, 50],
 'colesterol': [2.3, 2.5],
 'glucosa': [0.9, 1.5]
}
    
answered by 04.06.2018 в 00:35