这是您要求的另一种解决方案。
答案分为三部分:
- 扁平化输入字典
- 创建表(pandas DataFrame)
- 计算统计数据并构造输出
(要查看没有解释步骤的整个代码,请滚动到最底部。)
说明
扁平化输入字典是什么意思?答案很简单:一个不嵌套的字典,因此只有一维的键值对。
# Flat dictionary vs. nested dictionary
flat = {'a':1, 'b':2, 'c':3}
nested = {'a':1, 'b':{'c':2, 'd':3}} # 'b' has another dictionary as value
1.
# Flatten input dictionaries
# Following function returns a 1 dimensional dictionary where
# the before nested structure is still recognizable in its keys
# in the form parent.child.subchild...
def flatten(dic):
#
update = False
for key, val in dic.items():
if isinstance(val, dict):
update = True
break
if update:
val_key_tree = dict([(f'{key}.{k}', v) for k,v in val.items()])
dic.update(val_key_tree); dic.pop(key); flatten(dic)
return dic
# Example
windows1 = {"version": "windows 10",
"installed apps": {"chrome": "installed",
"python": {"python version": "2.7",
"folder": "c:\python27"},
"minecraft": "not installed"}}
flatten(windows1)
>>> {'version': 'windows 10',
'installed apps.chrome': 'installed',
'installed apps.minecraft': 'not installed',
'installed apps.python.python version': '2.7',
'installed apps.python.folder': 'c:\\python27'}
在键中引用嵌套结构将在稍后重新创建原始字典的结构时派上用场。
2.
# Create table (pandas DataFrame)
# With one dimensional dictionaries, it easy to create a pandas DataFrame where each row represents a dictionary
import pandas as pd
# Input
windows1 = {"version": "windows 10",
"installed apps": {"chrome": "installed",
"python": {"python version": "2.7",
"folder": "c:\python27"},
"minecraft": "not installed"}}
windows2 = {"version": "windows XP",
"installed apps": {"chrome": "not installed",
"python": {"python version": "not installed",
"folder": "c:\python27"},
"minecraft": "not installed"}}
dics = [windows1, windows2]
# Create DataFrame
frames = [pd.DataFrame(flatten(dic), index=[0]) for dic in dics]
df = pd.concat(frames, ignore_index=True)
df
>>>
3.
# Statistics
# Thanks to the DataFrame it is relatively simple to count how many times a value appears within a column
for c in df.columns: # iterate over dataframe columns
#
if 'folder' in c: # exclude certain columns (for example when 'folder' appears in column)
continue
uniques = df[c].unique() # all different values from a column
# Count how many times a value appears per column
counts = {}
for u in uniques:
tmp_u = u if not '\\' in u else u.replace('\\','\\\\') # backlash needs to be escaped specially
counts[u] = int(df[c].str.count('^'+tmp_u).sum()) # with the following str-method from dataframe
print(counts) # output from variable counts for each iteration
>>> {'windows 10': 1, 'windows XP': 1}
{'installed': 1, 'not installed': 1}
{'not installed': 2}
{'2.7': 1, 'not installed': 1}
从这里开始,我们可以重新创建原始字典的结构,因为我们在 DataFrame 列中有这些引用。
以下函数将创建一个嵌套字典,其结构类似于原始字典,并使用上面计算的统计信息:
# Recreate structured dictionary
def build_nested(struct, tree, res):
#
tree_split = tree.split('.',1)
try:
struct[tree_split[0]]
build_nested(struct[tree_split[0]], tree_split[-1], res)
except KeyError:
struct[tree_split[0]] = {}
if len(tree_split) < 2:
struct[tree_split[0]].update(res)
else:
struct[tree_split[0]][tree_split[1]] = {}
struct[tree_split[0]][tree_split[1]].update(res)
return struct
因此,我们可以将找到的属性传递给上面的函数 build_nested,而不是像上面第 3 节那样在每次迭代期间打印:
# Statistics
stats = {}
for c in df.columns:
#
if 'folder' in c:
continue
uniques = df[c].unique()
# Count how many times a value appears per column
counts = {}
for u in uniques:
tmp_u = u if not '\\' in u else u.replace('\\','\\\\')
counts[u] = int(df[c].str.count('^'+tmp_u).sum())
# Recreate the structure of nested dictionary
build_nested(stats, c, counts)
stats
>>>{'version': {'windows 10': 1, 'windows XP': 1},
'installed apps': {'chrome': {'installed': 1, 'not installed': 1},
'minecraft': {'not installed': 2},
'python': {'python version': {'2.7': 1, 'not installed': 1}}}}
完整代码
# Whole process put together
import json
import pandas as pd
# Helper functions
def flatten(dic):
#
update = False
for key, val in dic.items():
if isinstance(val, dict):
update = True
break
if update:
val_key_tree = dict([(f'{key}.{k}', v) for k,v in val.items()])
dic.update(val_key_tree); dic.pop(key); flatten(dic)
return dic
def build_nested(struct, tree, res):
#
tree_split = tree.split('.',1)
try:
struct[tree_split[0]]
build_nested(struct[tree_split[0]], tree_split[-1], res)
except KeyError:
struct[tree_split[0]] = {}
if len(tree_split) < 2:
struct[tree_split[0]].update(res)
else:
struct[tree_split[0]][tree_split[1]] = {}
struct[tree_split[0]][tree_split[1]].update(res)
return struct
# 1. & 2. Flatten input dictionaries and create table (pandas DataFrame)
windows1 = {"version": "windows 10",
"installed apps": {"chrome": "installed",
"python": {"python version": "2.7",
"folder": "c:\python27"},
"minecraft": "not installed"}}
windows2 = {"version": "windows XP",
"installed apps": {"chrome": "not installed",
"python": {"python version": "not installed",
"folder": "c:\python27"},
"minecraft": "not installed"}}
dics = [windows1, windows2]
frames = [pd.DataFrame(flatten(dic), index=[0]) for dic in dics]
df = pd.concat(frames, ignore_index=True)
# 3. Recreate nested dictionary with statistics
stats = {}
for c in df.columns:
#
if 'folder' in c:
continue
uniques = df[c].unique()
# Count how many times a value appears per column
counts = {}
for u in uniques:
tmp_u = u if not '\\' in u else u.replace('\\','\\\\')
counts[u] = int(df[c].str.count('^'+tmp_u).sum())
# Recreate the structure of nested dictionary
build_nested(stats, c, counts)
print(json.dumps(stats, indent=5))
>>>
{
"version": {
"windows 10": 1,
"windows XP": 1
},
"installed apps": {
"chrome": {
"installed": 1,
"not installed": 1
},
"minecraft": {
"not installed": 2
},
"python": {
"python version": {
"2.7": 1,
"not installed": 1
}
}
}
}