如何从包含重复值的列表制作词典
我是python的新手,我试图将以下列表转换为字典时苦苦挣扎了一段时间。如何从包含重复值的列表制作词典
lst = ['AB_MK1230', 'MK12303', 86.17, 'AB_MK1230', 'MK12302', 89.99,
'AB_MK1230', 'MK12301', 93.82, 'AB_MK1230', 'MK12301', 94.81, 'AB_MK1230',
'MK12303', 87.5, 'AB_MK1230', 'MK12302', 94.67, 'AB_MK1230', 'MK12302', 90.32,
'AB_MK1230', 'MK12303', 89.26, 'AB_MK1230', 'MK12301', 91.75,'AB_MK1230',
'MK12302', 88.54, 'AB_MK1230', 'MK12303', 92.5,'AB_MK1230', 'MK12301', 93.49,
'AB_MK1230', 'MK12301', 86.47,'AB_MK1230', 'MK12302', 84.79,'AB_MK1230',
'MK12303', 86.57,'AB_MK1230', 'MK12301', 79.24,'AB_MK1230', 'MK12302', 80.34,
'AB_MK1230', 'MK12303', 76.88]
AB_MK1230是母部门,MK12303,MK12301和MK12302是儿童部门。我的输出结果是有一个带有每个子扇区和父扇区的键的字典,并将浮点值作为该键的值,如下所示。
dict = {MK12301: AB_MK1230, 93.82, 94.81, 91.75, 93.49, 86.47, 79.24
MK12302: AB_MK1230, 89.99, 94.67, 90.32, 88.54, 84.79, 80.34
MK12303: AB_MK1230, 86.17, 87.50, 89.26, 92.50, 86.57, 76.88 }
我从https://docs.python.org/3/tutorial/datastructures.html?highlight=dictionaries阅读一些文档,但我仍然无法找出解决方案。
我该如何做到这一点?
我们重复列表,每次解包三个项目(parent, child, value)
-order。使用(parent, child)
作为字典中的关键字,然后将该值添加到set
中。
import itertools
import collections
lst = ['AB_MK1230', 'MK12303', 86.17, 'AB_MK1230', 'MK12302', 89.99,
'AB_MK1230', 'MK12301', 93.82, 'AB_MK1230', 'MK12301', 94.81, 'AB_MK1230',
'MK12303', 87.5, 'AB_MK1230', 'MK12302', 94.67, 'AB_MK1230', 'MK12302', 90.32,
'AB_MK1230', 'MK12303', 89.26, 'AB_MK1230', 'MK12301', 91.75,'AB_MK1230',
'MK12302', 88.54, 'AB_MK1230', 'MK12303', 92.5,'AB_MK1230', 'MK12301', 93.49,
'AB_MK1230', 'MK12301', 86.47,'AB_MK1230', 'MK12302', 84.79,'AB_MK1230',
'MK12303', 86.57,'AB_MK1230', 'MK12301', 79.24,'AB_MK1230', 'MK12302', 80.34,
'AB_MK1230', 'MK12303', 76.88]
d = collections.defaultdict(set)
for parent, child, value in itertools.zip_longest(*[iter(lst)]*3, fillvalue=None):
d[(parent, child)].add(value)
d现在就像是
defaultdict(set,
{('AB_MK1230', 'MK12301'): {79.24,
86.47,
91.75,
93.49,
93.82,
94.81},
('AB_MK1230', 'MK12302'): {80.34,
84.79,
88.54,
89.99,
90.32,
94.67},
('AB_MK1230', 'MK12303'): {76.88,
86.17,
86.57,
87.5,
89.26,
92.5}})
词典这不是完全清楚你的最终数据结构应该是怎样的模样。如果您只想parent
作为顶级密钥,则可以使用嵌套defaultdict
...
非常感谢!这就像一个魅力,这是我试图实现的解决方案。 –
我对这个数据结构有一个问题:它能够按照它们出现的顺序获取值吗?也就是说,{('AB_MK1230','MK12301'):93.82,94.81,91.75,93.49,86.47,79.24},(...,...):{...}} –
而不是'd = collections .defaultdict(set)'use'd = collections.defaultdict(list)'而不是'd [(parent,child)]。add(value)'use'd [(parent,child)]。append(value) ' – user2722968
[您尝试过什么?具体问题是什么? (c)(r)(tm)](https://meta.stackoverflow.com/questions/274630/should-we-add-a-do-my-work-for-me-close-reason) –
0)您的目标'dict'不是一个有效的Python数据结构1)每次迭代,从列表中读取3个项目并根据需要处理它们 –
但是,您提及的相同原理是否适用于父和子记录有所不同的100K记录?我没有把我试过的代码放在这里,因为它并不能让我对我的最终解决方案有任何线索。 –