简体   繁体   中英

How to convert Lists in dictionary in Python

I have two lists. size is same, there might be a none values in it.

I have added two list, one list is combination of two list.

I want to convert it into Dic.

I want M1 as key and second list as a Value.

I tried it, but Code reduced it's size

m1=['Date', 'PH', 'pC02', 'po2', 'cHCO3~', 'BE(ecf)', 'eSo2', 'Na+', 'K+', 'Cat+', 'ci-', 'cTco2', 'Het', 'cHgb', 'BE(b)', 'Glu', 'Lac', 'Crea', 'DH', 'pCo2', 'p02', 'ceHC03-', 'BE(ecf)', 'c$02', 'Na+', 'K+', 'Cat+', 'C1-', 'e1C02', 'Het', 'cHgb', 'BE(b)', 'Glu', 'Lac', 'Crea', 'Card', 'Last', 'Reader:', 'Host:', 'Sensor']

t1=[('22', '20'), ('7.465', None), ('29.0', None), ('181.0', None), ('20.9', None), ('2.9', None), ('99.7', None), ('131', None), ('2.6', None), ('0.41', None), ('101', None), ('21.8', None), ('32', None), ('10.9', None), ('2.0', None), ('38', None), ('0.30', None), ('0.57', None), ('7.350', '7.450'), ('35.0', '48.0'), ('83.0', '108.0'), ('21.0', '28.0'), ('2.0', '3.0'), ('94.0', '98.0'), ('138', '146'), ('3.5', '4.5'), ('1.15', '1.33'), ('98', '107'), ('22.0', '29'), ('38', '51'), ('12.0', '17.0'), ('2.0', '3.0'), ('74', '100'), ('0.56', '1.39'), ('9.51', '1.19'), ('01', '20033'), ('22', '20'), ('04205', '2.2'), ('10', '7'), ('35.3', None)]

d3={}
for i in range(len(m1)):
    d3[m1[i]]=t1[i]

actual length of both list is 40 but,

len(d3) becomes 30 and somevalues are missing

I am not able to find a problem. Thanks in advance

You're getting less entries in the dictionary since you have repeated values in m1 . This can be easily checked by building a set from the list and checking its length:

len(set(m1))
# 30

Note that a much simpler way to combine both lists into a dictionary would be to zip them and build a dictionary from the result:

d3 = dict(zip(m1,t1))

Which is essentially doing:

d3 = dict()
for k,v in zip(m1,t1):
    d3[k] = v

If you would like to capture all values then use a dictionary with list as value

Ex:

m1=['Date', 'PH', 'pC02', 'po2', 'cHCO3~', 'BE(ecf)', 'eSo2', 'Na+', 'K+', 'Cat+', 'ci-', 'cTco2', 'Het', 'cHgb', 'BE(b)', 'Glu', 'Lac', 'Crea', 'DH', 'pCo2', 'p02', 'ceHC03-', 'BE(ecf)', 'c$02', 'Na+', 'K+', 'Cat+', 'C1-', 'e1C02', 'Het', 'cHgb', 'BE(b)', 'Glu', 'Lac', 'Crea', 'Card', 'Last', 'Reader:', 'Host:', 'Sensor']
t1=[('22', '20'), ('7.465', None), ('29.0', None), ('181.0', None), ('20.9', None), ('2.9', None), ('99.7', None), ('131', None), ('2.6', None), ('0.41', None), ('101', None), ('21.8', None), ('32', None), ('10.9', None), ('2.0', None), ('38', None), ('0.30', None), ('0.57', None), ('7.350', '7.450'), ('35.0', '48.0'), ('83.0', '108.0'), ('21.0', '28.0'), ('2.0', '3.0'), ('94.0', '98.0'), ('138', '146'), ('3.5', '4.5'), ('1.15', '1.33'), ('98', '107'), ('22.0', '29'), ('38', '51'), ('12.0', '17.0'), ('2.0', '3.0'), ('74', '100'), ('0.56', '1.39'), ('9.51', '1.19'), ('01', '20033'), ('22', '20'), ('04205', '2.2'), ('10', '7'), ('35.3', None)]

d3 = {}
for k, v in zip(m1, t1):
    d3.setdefault(k, []).append(v)  #You can also use collections.defaultdict here
print(d3)

Output:

{'BE(b)': [('2.0', None), ('2.0', '3.0')],   #note multiple values for single key
 'BE(ecf)': [('2.9', None), ('2.0', '3.0')],  #note multiple values for single key
 'C1-': [('98', '107')],
 'Card': [('01', '20033')],
 'Cat+': [('0.41', None), ('1.15', '1.33')],
 'Crea': [('0.57', None), ('9.51', '1.19')],
 'DH': [('7.350', '7.450')],
 'Date': [('22', '20')],
 'Glu': [('38', None), ('74', '100')],
 'Het': [('32', None), ('38', '51')],
 'Host:': [('10', '7')],
 'K+': [('2.6', None), ('3.5', '4.5')],
 'Lac': [('0.30', None), ('0.56', '1.39')],
 'Last': [('22', '20')],
 'Na+': [('131', None), ('138', '146')],
 'PH': [('7.465', None)],
 'Reader:': [('04205', '2.2')],
 'Sensor': [('35.3', None)],
 'c$02': [('94.0', '98.0')],
 'cHCO3~': [('20.9', None)],
 'cHgb': [('10.9', None), ('12.0', '17.0')],
 'cTco2': [('21.8', None)],
 'ceHC03-': [('21.0', '28.0')],
 'ci-': [('101', None)],
 'e1C02': [('22.0', '29')],
 'eSo2': [('99.7', None)],
 'p02': [('83.0', '108.0')],
 'pC02': [('29.0', None)],
 'pCo2': [('35.0', '48.0')],
 'po2': [('181.0', None)]}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM