小编Tho*_*las的帖子

从拼合字典创建嵌套字典

我有一个扁平的字典,我想把它变成一个嵌套的字典

flat = {'X_a_one': 10,
        'X_a_two': 20, 
        'X_b_one': 10,
        'X_b_two': 20, 
        'Y_a_one': 10,
        'Y_a_two': 20,
        'Y_b_one': 10,
        'Y_b_two': 20}
Run Code Online (Sandbox Code Playgroud)

我想将其转换为表单

nested = {'X': {'a': {'one': 10,
                      'two': 20}, 
                'b': {'one': 10,
                      'two': 20}}, 
          'Y': {'a': {'one': 10,
                      'two': 20},
                'b': {'one': 10,
                      'two': 20}}}
Run Code Online (Sandbox Code Playgroud)

扁平字典的结构使得模糊不应存在任何问题.我希望它适用于任意深度的字典,但性能并不是真正的问题.我已经看到很多用于展平嵌套字典的方法,但基本上没有用于嵌套扁平字典的方法.存储在字典中的值是标量或字符串,永远不会迭代.

到目前为止,我有一些可以接受输入的东西

test_dict = {'X_a_one': '10',
             'X_b_one': '10',
             'X_c_one': '10'}
Run Code Online (Sandbox Code Playgroud)

到输出

test_out = {'X': {'a_one': '10', 
                  'b_one': '10', 
                  'c_one': '10'}}
Run Code Online (Sandbox Code Playgroud)

使用代码

def nest_once(inp_dict):
    out = {}
    if isinstance(inp_dict, dict):
        for key, val in inp_dict.items():
            if '_' …
Run Code Online (Sandbox Code Playgroud)

python recursion dictionary nested netcdf

50
推荐指数
3
解决办法
2842
查看次数

使用xarray + dask的内存错误 - 使用groupby或apply_ufunc?

我使用xarray作为分析流体湍流数据的工作流程的基础,但我无法正确利用dask来限制笔记本电脑上的内存使用量.

我有一个n带维度的数据阵列('t', 'x', 'z'),我在z维度上将其拆分为5个块:

<xarray.DataArray 'n' (t: 801, x: 960, z: 512)>
dask.array<shape=(801, 960, 512), dtype=float32, chunksize=(801, 960, 5)>
Coordinates:
* t              (t) int64 200 201 202 203 204 205 206 207 208 209 210 211 ...
* x              (x) int64 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
* z              (z) int64 0 1 2 3 4 5 6 7 8 9 10 11 …
Run Code Online (Sandbox Code Playgroud)

python out-of-memory dask python-xarray pandas-groupby

6
推荐指数
1
解决办法
546
查看次数

xarray 的 apply_ufunc 中的 dask=parallelized 和 dask=allowed 有什么区别?

在函数 apply_ufunc的xarray 文档中,它说:

dask: ‘forbidden’, ‘allowed’ or ‘parallelized’, optional

    How to handle applying to objects containing lazy data in the form of dask arrays:

    ‘forbidden’ (default): raise an error if a dask array is encountered.
    ‘allowed’: pass dask arrays directly on to func.
    ‘parallelized’: automatically parallelize func if any of the inputs are a dask array. 
                    If used, the output_dtypes argument must also be provided. 
                    Multiple output arguments are not yet supported.
Run Code Online (Sandbox Code Playgroud)

Parallel Computing的文档页面中,有一个注释:

对于大多数已经由 dask …

python numpy numpy-ufunc dask python-xarray

5
推荐指数
1
解决办法
374
查看次数

协议的所有基础都必须是协议——typing.MutableMapping 不是协议吗?

我想为自定义映射类定义一个类型协议。这个类需要与 a 非常相似,除了它除了那些定义为抽象方法(特别是)MutableMapping之外还有几个附加方法,并且我还想提前指定自定义映射类的任何实现必须使用哪些类型作为键和值。collections.abc.MutableMapping.copy()

读完PEP 544后,我想我应该能够做到这一点:

from typing import Hashable, MutableMapping, Protocol, TypeVar


TVarMapProt = TypeVar("TVarMapProt", bound="VariableMappingProtocol")


class VariableMappingProtocol(MutableMapping[Hashable, int], Protocol):
    """
    Protocol for the type behaviour of a class which
    acts basically like a dict of int objects.
    """

    def copy(self: TVarMapProt) -> TVarMapProt:
         # TODO replace return type with Self if PEP 673 goes through
        ...
Run Code Online (Sandbox Code Playgroud)

我的想法是,在我的代码中,我可以声明需要一个类型VariableMappingProtocol,然后用户必须使用自己定义的类,以避免输入错误:

TCusVarMap = TypeVar("CusVarMap", bound="CustomVariableMapping")


class CustomVariableMapping
    """Defines all the methods that a MutableMapping does, …
Run Code Online (Sandbox Code Playgroud)

python abc structural-typing mypy python-typing

5
推荐指数
0
解决办法
568
查看次数

为什么 numpy.angle() 不是 ufunc?

为什么numpy.angle()不是一个 numpy 通用函数(ufunc)?

它似乎符合numpy 文档中ufunc 的标准,但未列为其中之一。

我认为它可能不符合定义,因为它转换了数字的类型(从复数到实数),但是已经有其他 ufunc 可以做到这一点(例如np.absolute)。

我遇到这个是因为我试图np.angle直接应用于包含复数的 xarray DataArray,它返回一个 numpy 数组而不是 xarray DataArray。我认为它这样做是因为它不是一个 numpy ufunc,并且 xarray 会检查它。

python numpy python-xarray

2
推荐指数
1
解决办法
175
查看次数