[英]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.我想把它转换成 Dic。
I want M1 as key and second list as a Value.我希望 M1 作为键,第二个列表作为值。
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,两个列表的实际长度都是 40,但是,
len(d3) becomes 30 and somevalues are missing len(d3) 变为 30 并且缺少某些值
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
.由于您在
m1
中有重复的值,因此您在字典中获得的条目较少。 This can be easily checked by building a set
from the list and checking its length:这可以通过从列表中构建一个
set
并检查其长度来轻松检查:
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:请注意,将两个列表合并到字典中的一种更简单的方法是
zip
它们并根据结果构建字典:
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如果您想捕获所有值,请使用以
list
作为值的字典
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: 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)]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.