[英]Creating dynamic nested dictionary of counts
I have a text file abc.txt
: 我有一个文本文件
abc.txt
:
abc/pqr/lmn/xyz:pass
abc/pqr/lmn/bcd:pass
I need to parse these statements and output should be in nested dictionary as below: 我需要解析这些语句,并且输出应位于嵌套字典中,如下所示:
{'abc':{'pqr':{'lmn':{'xyz':{'pass':1},{'bcd':{'pass':1}}}}}}
where 1 is 'pass'
count. 其中1是
'pass'
计数。
I'm able to do as much as this: 我能够做到这一点:
import re
d={}
p=re.compile('[a-zA-z]+')
for line in open('abc.txt'):
for key in p.findall(line):
d['key']={}
Here's an updated version of my answer in which leaves of the tree data-structure are now different from those in rest of it. 这是我的答案的更新版本,其中树数据结构的叶子现在与其余部分的叶子不同。 Instead of the tree being strictly a
dict
-of-nested- dict
s, the "leaves" on each branch are now instances of a different subclass of dict
named collections.Counter
which are useful for counting the number of times each of their keys occur. 取而代之的是树一个严格的
dict
-of-nested- dict
秒,“叶子”上的每个分支现在不同的子类的实例dict
命名collections.Counter
这对于每个计数的键出现的次数非常有用。 I did this because of your response to my question about what should happen if the last part of each line was something other than ":pass"
(which was "we have to put new count for that key"). 我之所以这样做是因为你回答了我的问题,如果每一行的最后一部分不是
":pass"
(这是“我们必须为该键设置新计数”),应该会发生什么。
Nested dictionaries are often called Tree
data-structures and can be defined recursively — the root is a dictionary as are the branches. 嵌套字典通常称为
Tree
数据结构,可以递归定义 - 根是字典,分支也是字典。 The following uses a dict
subclass instead of a plain dict
because it makes constructing them easier since you don't need to special case the creation of the first branch of next level down (except I still do when adding the "leaves" because they are a different subclass, collections.Counter
). 下面使用一个
dict
子类而不是一个普通的dict
因为它使构造它们更容易,因为你不需要特殊情况下创建下一级的第一个分支(除了我仍然在添加“叶子”时,因为它们是一个不同的子类collections.Counter
)。
from collections import Counter
from functools import reduce
import re
# (Optional) trick to make Counter subclass print like a regular dict.
class Counter(Counter):
def __repr__(self):
return dict(self).__repr__()
# Borrowed from answer @ https://stackoverflow.com/a/19829714/355230
class Tree(dict):
def __missing__(self, key):
value = self[key] = type(self)()
return value
# Utility functions based on answer @ https://stackoverflow.com/a/14692747/355230
def nested_dict_get(nested_dict, keys):
return reduce(lambda d, k: d[k], keys, nested_dict)
def nested_dict_set(nested_dict, keys, value):
nested_dict_get(nested_dict, keys[:-1])[keys[-1]] = value
def nested_dict_update_count(nested_dict, keys):
counter = nested_dict_get(nested_dict, keys[:-1])
if counter: # Update existing Counter.
counter.update([keys[-1]])
else: # Create a new Counter.
nested_dict_set(nested_dict, keys[:-1], Counter([keys[-1]]))
d = Tree()
pat = re.compile(r'[a-zA-z]+')
with open('abc.txt') as file:
for line in file:
nested_dict_update_count(d, [w for w in pat.findall(line.rstrip())])
print(d) # Prints like a regular dict.
To test the leaf-counting capabilities of the revised code, I used the following test file which includes the same line twice, once ending again with :pass
and another ending in :fail
. 为了测试修改后的代码的叶子计数功能,我使用了以下测试文件,其中包含两次相同的行,一次以
:pass
结束,另一次以:fail
结束。
Expanded abc.txt
test file: 扩展的
abc.txt
测试文件:
abc/pqr/lmn/xyz:pass
abc/pqr/lmn/bcd:pass
abc/pqr/lmn/xyz:fail
abc/pqr/lmn/xyz:pass
Output: 输出:
{'abc': {'pqr': {'lmn': {'bcd': {'pass': 1}, 'xyz': {'fail': 1, 'pass': 2}}}}}
Check out the setdefault
method on dictionaries. 查看字典上的
setdefault
方法。
d = {}
d.setdefault('pqr', {}).setdefault('lmn', {}).setdefault('xyz', {})['pass'] = 1
d.setdefault('pqr', {}).setdefault('lmn', {}).setdefault('bcd', {})['pass'] = 1
d
gives 给
{'pqr': {'lmn': {'bcd': {'pass': 1}, 'xyz': {'pass': 1}}}}
If i understand your question: 如果我了解您的问题:
sources = ["abc/pqr/lmn/xyz:pass", "abc/pqr/lmn/bcd:pass", "abc/pqr/lmn/xyz:pass"]
def prepare_source(source):
path, value = source.split(':')
elements = path.split('/')
return elements, value
def add_key(elements, value):
result = dict()
if len(elements) > 1:
result[elements[0]] = add_key(elements[1:], value)
else:
result[elements[0]] = {value: 1}
return result
# base merge function get from here:
# http://stackoverflow.com/questions/7204805/dictionaries-of-dictionaries-merge
def merge(a, b, path=None):
"merges b into a"
if path is None: path = []
for key in b:
if key in a:
if isinstance(a[key], dict) and isinstance(b[key], dict):
merge(a[key], b[key], path + [str(key)])
elif isinstance(a[key], int) and isinstance(b[key], int):
a[key] += b[key]
else:
raise Exception('Conflict at %s' % '.'.join(path + [str(key)]))
else:
a[key] = b[key]
return a
result = dict()
for source in sources:
result = merge(result, add_key(*prepare_source(source)))
print result
Output will be: 输出将是:
{'abc': {'pqr': {'lmn': {'bcd': {'pass': 1}, 'xyz': {'pass': 2}}}}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.