【发布时间】:2013-12-01 06:59:28
【问题描述】:
我有一本字典,其中包含n 的各种值的各种协议的模拟结果(“协议”和n 与我面临的问题无关)。本词典结构如下:
myDict = {"protocol1" : {1:[some list of numbers], 2:[another list of numbers]},
"protocol2" : {1:[some list of numbers], 2:[another list of numbers]},
}
现在,为了分析结果,我会这样做:
for protocol, stats in myDict.items():
for n, counts in stats.items():
# do stuff with protocol, n and counts
不过我想知道,是否存在一些内置插件可以让我这样做,而无需定义自定义迭代器:
for protocol, n, counts in magicFunc(myDict):
# do stuff with protocol, n and counts
itertools 中可能有什么东西可以让我这样做吗?
【问题讨论】:
-
就目前而言,我会坚持你所拥有的。如果您使用第二个 dict,而不是具有更多 dicts 的列表,那么实际上,您将使用 dict flattener 并从中产生。否则,针对您当前问题的任何解决方案都将变得更加晦涩难懂且不值得……
-
you'd use a dict flattener。有这样的东西存在吗(很感兴趣),还是必须自己写(不太感兴趣)? -
举了一个简单的例子来说明我的意思......如果你嵌套得更深,它可能会有一些价值 - 否则 - 坚持你得到的......
标签: python python-3.x dictionary python-3.3