小编zin*_*aai的帖子

使用空的numpy数组创建一个defaultdict

我想知道是否有更聪明的方法来从集合中创建默认字典.dict应该有一个空的numpy ndarray作为默认值.

到目前为止,我最好的结果是:

import collections
d = collections.defaultdict(lambda: numpy.ndarray(0))
Run Code Online (Sandbox Code Playgroud)

但是,我想知道是否有可能跳过lambda术语并以更直接的方式创建dict.喜欢:

d = collections.defaultdict(numpy.ndarray(0))  # <- Nice and short - but not callable
Run Code Online (Sandbox Code Playgroud)

python collections numpy defaultdict

9
推荐指数
1
解决办法
6975
查看次数

标签 统计

collections ×1

defaultdict ×1

numpy ×1

python ×1