使用python将分隔符串的列表转换为树/嵌套的dict [英] convert a list of delimited strings to a tree/nested dict, using python

查看:528
本文介绍了使用python将分隔符串的列表转换为树/嵌套的dict的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试转换点分隔字符串列表,例如

  ['one.two.three.four ','one.six.seven.eight','five.nine.ten','12.zero'] 

成一棵树(嵌套的列表或dicts - 任何容易走过的东西)。
真正的数据恰好有1到4个不同长度的点分隔的部分,总共有2200条记录。
我的实际目标是用这个数据填写4个QComboBox的集合,方式是第一个QComboBox填充第一个项目['one','five','12'](不重复)。然后根据所选项目,第二个QComboBox填入相关项目:对于'one',它将是:['two','six']等等,如果有另一个嵌套级别。



到目前为止,我有一个工作列表 - >嵌套的dicts解决方案,但这是非常慢的,因为我使用常规的dict()。我似乎有一个麻烦,重新设计为一个默认的方式,以轻松地填充ComboBoxes正确。



我当前的代码:

  def list2tree(m):
tmp = {}
for i in range(len(m)):
如果m.count('。')== 0:
return m
a = m.split('。',1)
try:
tmp [a [0 ]]。append(list2tree(a [1]))
except(KeyError,AttributeError):
tmp [a [0]] = list2tree(a [1])$ ​​b $ b return tmp

main_dict = {}
i = 0
在方法中的m:
main_dict = list2tree(m)
i + = 1
if(i %100)== 0:print i,len(methods)
print main_dict,i,len(methods)


解决方案

  ls = ['one.two.three.four','one.six.seven.eight','five .nine.ten','12.zero'] 
tree = {}

为ls中的项目:
t = tree
为item.spl中的一部分它('。'):
t = t.setdefault(part,{})

结果:

  {
十二:{
零:{}
},
五:{
九:{
十:{}
}
},

six:{
seven:{
eight:{}
}
},
two:{
three:{
four:{}
}
}
}
}
pre>

I am trying to convert a list of dot-separated strings, e.g.

['one.two.three.four', 'one.six.seven.eight', 'five.nine.ten', 'twelve.zero']

into a tree (nested lists or dicts - anything that is easy to walk through). The real data happens to have 1 to 4 dot-separated parts of different length and has 2200 records in total. My actual goal is to fill in the set of 4 QComboBox'es with this data, in manner that the 1st QComboBox is filled with first set items ['one', 'five', 'twelve'] (no duplicates). Then depending on the chosen item, the 2nd QComboBox is filled with its related items: for 'one' it would be: ['two', 'six'], and so on, if there's another nested level.

So far I've got a working list -> nested dicts solution, but it's horribly slow, since I use regular dict(). And I seem to have a trouble to redesign it to a defaultdict in a way to easily work out filling the ComboBoxes properly.

My current code:

def list2tree(m):
    tmp = {}
    for i in range(len(m)):
        if m.count('.') == 0:
            return m
        a = m.split('.', 1)
        try:
            tmp[a[0]].append(list2tree(a[1]))
        except (KeyError, AttributeError):
            tmp[a[0]] = list2tree(a[1])
    return tmp

main_dict = {}
i = 0
for m in methods:
    main_dict = list2tree(m)
    i += 1
    if (i % 100) == 0: print i, len(methods)
print main_dict, i, len(methods)

解决方案

ls = ['one.two.three.four', 'one.six.seven.eight', 'five.nine.ten', 'twelve.zero']
tree = {}

for item in ls:
    t = tree
    for part in item.split('.'):
        t = t.setdefault(part, {})

Result:

{
 "twelve": {
  "zero": {}
 }, 
 "five": {
  "nine": {
   "ten": {}
  }
 }, 
 "one": {
  "six": {
   "seven": {
    "eight": {}
   }
  }, 
  "two": {
   "three": {
    "four": {}
   }
  }
 }
}

这篇关于使用python将分隔符串的列表转换为树/嵌套的dict的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆