我有这样的字典:
{'CO,': {u'123456': [55111491410]},
u'OA,': {u'3215': [55111400572]},
u'KO,': {u'asdas': [55111186735],u'5541017924': [55111438755]},
u'KU': {u'45645': [55111281815],u'546465238': [55111461870]},
u'TU': {u'asdfds': [55111161462],u'546454149': [55111128782],
u'546454793': [55111167133],u'546456387': [55111167139],
u'546456925': [55111167140],u'546458931': [55111226912],
u'546458951': [55111226914],u'546459861': [55111226916],
u'546460165': [55111403171, 55111461858]}}
我想获取嵌套字典中所有列表的合并列表。
输出应如下所示:
[55111491410,55111400572,55111186735,55111438755,55111281815,55111461870,55111167133,55111167139,....55111403171,55111461858]
答案 0 :(得分:2)
基于正则表达式和所有感兴趣的值都放在方括号中的事实,这是一个优雅的答案
import re
pat = r'(?<=\[).+?(?=\])'
s = """{'CO,': {u'123456': [55111491410]},
u'OA,': {u'3215': [55111400572]},
u'KO,': {u'asdas': [55111186735],u'5541017924': [55111438755]},
u'KU': {u'45645': [55111281815],u'546465238': [55111461870]},
u'TU': {u'asdfds': [55111161462],u'546454149': [55111128782],
u'546454793': [55111167133],u'546456387': [55111167139],
u'546456925': [55111167140],u'546458931': [55111226912],
u'546458951': [55111226914],u'546459861': [55111226916],
u'546460165': [55111403171, 55111461858]}}"""
print('[%s]' % ', '.join(map(str, re.findall(pat, s))))
输出
[55111491410, 55111400572, 55111186735, 55111438755, 55111281815, 55111461870, 55111161462, 55111128782, 55111167133, 55111167139, 55111167140, 55111226912, 55111226914, 55111226916, 55111403171, 55111461858]
答案 1 :(得分:1)
x仅使用dict的值和内部dict的值来进行列表理解就可以了。但请记住,直到3.6才在python中对字典进行排序。因此,如果您使用的是较早的版本,则结果列表也将没有任何顺序
>>> dct = {'CO,': {u'123456': [55111491410]},
... u'OA,': {u'3215': [55111400572]},
... u'KO,': {u'asdas': [55111186735],u'5541017924': [55111438755]},
... u'KU': {u'45645': [55111281815],u'546465238': [55111461870]},
... u'TU': {u'asdfds': [55111161462],u'546454149': [55111128782],
... u'546454793': [55111167133],u'546456387': [55111167139],
... u'546456925': [55111167140],u'546458931': [55111226912],
... u'546458951': [55111226914],u'546459861': [55111226916],
... u'546460165': [55111403171, 55111461858]}}
>>>
>>> [e for idct in dct.values() for lst in idct.values() for e in lst]
[55111491410, 55111400572, 55111186735, 55111438755, 55111281815, 55111461870, 55111161462, 55111128782, 55111167133, 55111167139, 55111167140, 55111226912, 55111226914, 55111226916, 55111403171, 55111461858]
答案 2 :(得分:0)
d = {'CO,': {u'123456': [55111491410]},
u'OA,': {u'3215': [55111400572]},
u'KO,': {u'asdas': [55111186735], u'5541017924': [55111438755]},
u'KU': {u'45645': [55111281815], u'546465238': [55111461870]},
u'TU': {u'asdfds': [55111161462], u'546454149': [55111128782],
u'546454793': [55111167133], u'546456387': [55111167139],
u'546456925': [55111167140], u'546458931': [55111226912],
u'546458951': [55111226914], u'546459861': [55111226916],
u'546460165': [55111403171, 55111461858]}}
z = []
for i in d.keys():
for j in d[i].keys():
z.append(d[i][j][0])
print(z)
输出:
[55111491410, 55111400572, 55111186735, 55111438755, 55111281815, 55111461870, 55111161462, 55111128782, 55111167133, 55111167139, 55111167140, 55111226912, 55111226914, 55111226916, 55111403171]