简体   繁体   English

如何创建列表的嵌套字典

[英]how to create a nested dictionary of list

I have a dictionary like this:我有一本这样的字典:

mydic = {
  0: [('comp.sys.mac.hardware_51563', 0.31), ('rec.motorcycles_103140', 0.29), ('alt.atheism_54143', 0.27), ('misc.forsale_76672', 0.26), ('rec.motorcycles_104809', 0.26)], 
  1: [('alt.atheism_54143', 0.31), ('comp.sys.mac.hardware_51563', 0.30), ('rec.motorcycles_104885', 0.30), ('talk.religion.misc_84064', 0.28), ('rec.sport.baseball_104619', 0.27)], 
  2: [('alt.atheism_54143', 0.29), ('talk.religion.misc_84064', 0.29), ('rec.sport.hockey_53859', 0.26), ('sci.electronics_54162', 0.25), ('comp.sys.mac.hardware_52124', 0.25)]
}

What I want to do is to cut the digits in the last part of string and then count the frequency of them per key.我想要做的是剪切字符串最后一部分的数字,然后计算每个键的频率。 However I need to keep the order of them as well to have the result like this:但是,我还需要保持它们的顺序才能得到这样的结果:

{
  0: {'comp.sys.mac.hardware': [1, 1], 'rec.motorcycles': [2,2], 'alt.atheism': [1,3], 'misc.forsale': [1, 4]}, 
  1: {'alt.atheism': [1,1], 'comp.sys.mac.hardware': [1,2], 'rec.motorcycles': [1,3], 'talk.religion.misc': [1,4], 'rec.sport.baseball': [1,5]}, 
  2: {'alt.atheism': [1,1], 'talk.religion.misc': [1,2], 'rec.sport.hockey': [1,3], 'sci.electronics': [1,4], 'comp.sys.mac.hardware': [1,5]}
}

here 'comp.sys.mac.hardware': [1, 1] means comp.sys.mac.hardware has frequency 1 and is the first thing in the initial list.这里'comp.sys.mac.hardware': [1, 1]表示comp.sys.mac.hardware的频率为1并且是初始列表中的第一件事。 Or 'alt.atheism': [1,3] means has been repeated 1 time and is the third item in the original list.'alt.atheism': [1,3]表示已重复1次,是原始列表中的第三项。

I have worked on this and so far have come up with a result using the code below.我已经对此进行了研究,到目前为止,我已经使用下面的代码得出了一个结果。 My problem is that I cannot include the order in the dictionary, :我的问题是我不能在字典中包含order ,:

{
  0: {'comp.sys.mac.hardware': 1, 'rec.motorcycles': 2, 'alt.atheism': 1, 'misc.forsale': 1}, 
  1: {'alt.atheism': 1, 'comp.sys.mac.hardware': 1, 'rec.motorcycles': 1, 'talk.religion.misc': 1, 'rec.sport.baseball': 1}, 
  2: {'alt.atheism': 1, 'talk.religion.misc': 1, 'rec.sport.hockey': 1, 'sci.electronics': 1, 'comp.sys.mac.hardware': 1}
}


freq_dic = collections.defaultdict(dict)
for k, va in mydic.items():
    lst = []
    for m in va:
        el = m[0].split('_')[0]
        if el in freq_dic[k]:
            freq_dic[k][el] = freq_dic[k][el] + 1
        else:
            freq_dic[k][el] = 1

You need only to augment your iteration, and you will have order available within the loop.您只需要增加您的迭代,并且您将在循环中获得可用的order

for order, m in enumerate(va,1):
    el = m[0].split('_')[0]
    if el in freq_dic[k]:
        freq_dic[k][el][0] += 1
    else:
        freq_dic[k][el] = [1, order]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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