Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert a list of delimited strings to a tree/nested dict, using python

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)
like image 984
python_head Avatar asked May 14 '13 15:05

python_head


People also ask

Can you convert a list to a dictionary in Python?

You can convert a Python list to a dictionary using a dictionary comprehension, dict. fromkeys(), or the zip() method. All three methods create a new dictionary. They do not modify the existing list.

How do you make a nested dictionary dynamically in Python?

In Python, a Nested dictionary can be created by placing the comma-separated dictionaries enclosed within braces.

How do you get a value from a nested dictionary in a list in Python?

Access Values using get() Another way to access value(s) in a nested dictionary ( employees ) is to use the dict. get() method. This method returns the value for a specified key. If the specified key does not exist, the get() method returns None (preventing a KeyError ).

How do you access a dictionary nested in a list?

You can make use of the eval function in python. nestq will be "nest['b']['v']['y']" where nest is the nested dictionary. The eval builtin function executes the given string. However, it is important to be careful about possible vulnerabilities that arise from use of eval function.


1 Answers

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": {}
   }
  }
 }
}
like image 196
georg Avatar answered Sep 21 '22 02:09

georg