- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个在 Python 中接收嵌套字典的进程:
嵌套字典架构示例(伪代码)
key1: value1,
key2: dict(
key3: value2,
key4: value3,
),
key5: list(value4,value5) # any value is fine, just not empty or null
嵌套字典数据示例(伪代码)
key1: 'value',
key2: dict(
key3: '',
key4: 12345,
),
key5: list()
我想迭代/扫描这个字典并检查每个键是否有值(不为空或空白 - false/0 都可以)。我需要扫描一堆相同的字典以获得该组字典的总体“填充率”。该过程每次运行时都会看到不同格式的字典集,因此需要自动生成填充率报告:
上面单个嵌套示例的示例填充率(理想情况下是平面字典):
key1: 1
key2: 1
key2-key3: 0
key2-key4: 1
key5: 0
例如,如果我们扫描具有相同结构的十个字典,我们可能会看到如下所示的“填充率”:
key1: 5
key2: 6
key2-key3: 6
key2-key4: 4
key5: 3
问题
扫描不同结构的字典以生成填充率的最Pythonic方法是什么?如果我必须执行数百万次,是否有更有效的方法?
创建平面字典来存储计数的最 Pythonic 方法是什么以及如何更新它?
最佳答案
这是我的看法:
What is the most pythonic way to scan dicts of varying structure to gen a fill rate?
递归地。特别是,我将遍历子树的结果返回给调用者。调用者负责将多个子树合并到自己的树结果中。
Is there a more efficient way if I have to do this millions of times?
也许吧。尝试一种解决方案,看看它是否 A) 正确且 B) 足够快。如果两者兼而有之,就不必费心寻找最有效的方法。
What is the most pythonic way to create a flat dict to store the counts and how do I update it?
通过使用 Python 附带的库之一。在本例中,为collections.Counter()
。并通过调用其 .update()
函数。
from collections import Counter
from pprint import pprint
example1_dict = {
'key1': 'value',
'key2': {
'key3': '',
'key4': 12345,
},
'key5': list()
}
example2_dict = {
'key1': 'value',
'key7': {
'key3': '',
'key4': 12345,
},
'key5': [1]
}
def get_fill_rate(d, path=()):
result = Counter()
for k, v in d.items():
if isinstance(v, dict):
result[path+(k,)] += 1
result.update(get_fill_rate(v, path+(k,)))
elif v in (False, 0):
result[path+(k,)] += 1
elif v:
result[path+(k,)] += 1
else:
result[path+(k,)] += 0
return result
def get_fill_rates(l):
result = Counter()
for d in l:
result.update(get_fill_rate(d))
return dict(result)
result = get_fill_rates([example1_dict, example2_dict])
# Raw result
pprint(result)
# Formatted result
print('\n'.join(
'-'.join(single_key for single_key in key) + ': ' + str(value)
for key, value in sorted(result.items())))
结果:
{('key1',): 2,
('key2',): 1,
('key2', 'key3'): 0,
('key2', 'key4'): 1,
('key5',): 1,
('key7',): 1,
('key7', 'key3'): 0,
('key7', 'key4'): 1}
key1: 2
key2: 1
key2-key3: 0
key2-key4: 1
key5: 1
key7: 1
key7-key3: 0
key7-key4: 1
关于python - Python 中不同嵌套字典的 Gen 填充率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48452708/
第 0 代:所以我读到当 gen 0 超过大小阈值时 gen 0 触发。但我也读到GC不是在"new"时执行的,因为"new"只是向前滑动一个地址。那么,它什么时候检查它是否超过了触发 gen 0 G
“static final”是直接分配到young gen还是old gen还是perm gen? (我想它很可能会在我想的时间内落入老一代。)如果它在 perm gen 中分配,那么当在 Perm
这是一种情况,当必须将并行性引入后端服务器时。 我愿意查询 N 个 ELB,每个查询 5 个不同的查询,并将结果发送回 Web 客户端。 后端是Tornado,根据我在docs中多次阅读的内容,在过去
我正在尝试创建列表的随机排列。我是函数式语言中随机性的新手,还没有完全掌握 monad,但我以我认为应该有效的方式使用了 Random.newStdGen 和 Random.Shuffle.shuff
经过 tornado.gen documentation有人可以帮我理解 tornado.gen.coroutine 和 tornado.gen.engine 之间的确切区别 最佳答案 正如 gen.
在 document , 如果方法也用@gen.coroutine 修饰,则不需要@web.asynchronous。像这样 @web.asynchronous @gen.coroutine def
我们在带有相量的同一个 gen~ 代码框对象中有两个地方: wander = phasor(in8/dense); ...some code later... phas = (triangle(pha
我正在从事的项目都是用 Tornado 编写的,但我包含了一些 Twisted 来处理异步 XML-RPC。我想知道您是否可以使用 Tornado 的 gen.engine 和 yield gen.T
如何查看protoc-gen-go和protoc-gen-validate的版本? protoc-gen-go --version protoc-gen-validate --version 我试过上
我们使用的是 protoc-gen-go v2(v2 是 google.golang.org/protobuf,v1 是 github.com/golang/protobuf) 当我们尝试编译 gRP
刚接触tornado,所以跟着tornado的指导练习,当我开始使用Coroutines的时候,例子是这样说的: 从 Tornado 导入生成 @gen.coroutine def fetch_cor
当您创建 Python 3 生成器并立即开始运行它时。您会收到如下错误: TypeError: can't send non-None value to a just-started generato
当我在 eclipse 中清理和构建 android 项目时,它会删除 bin 文件夹和 gen 文件夹的内容,并且不会重新创建它只是创建空文件夹 gen 和 bin 为什么会发生这种情况我搜索了很多
我想使用来自 protoc-gen-grpc-kotlin 的 protoc-gen-grpc-kotlin-1.0.0我得到了 Execution failed for task ':generat
我想使用来自 protoc-gen-grpc-kotlin 的 protoc-gen-grpc-kotlin-1.0.0我得到了 Execution failed for task ':generat
在 :constructors 映射和后续的 -init 定义中,我如何表示可变参数构造函数(假设父类(super class)有多个构造函数,其中一个是可变参数)? 最佳答案 由于可变参数本质上是对
我正在使用jwilder/nginx-proxy,以便在我的domain.tld中有一个项目,在api.domain.tld中有一个项目。我关注了this tutorial,自定义default.co
我刚刚重新格式化了我的电脑。我有 windows xp sp3 32 位。我安装java jdk安卓SDK eclipse ide 当我去创建一个新的 android 项目时,我得到了这个错误。 项目
我正在开发我的 Android 项目,删除未使用的库后,我收到 错误: myproject/gen already exists but is not a source folder. Convert
protoc-gen-doc 自定义模板规则详解 配套演示工程 此项目中所用 proto 文件位于 ./proto 目录下,来源于 官方proto示例 此项目中所列所有
我是一名优秀的程序员,十分优秀!