【问题标题】:dict 'type' object is not subscriptable when declaring a dict type variable声明 dict 类型变量时,dict 'type' 对象不可下标
【发布时间】:2022-07-28 06:05:42
【问题描述】:

我正在尝试定义一个包含列表的字典:

import numpy as np

def processing_stitch_visual_outputs(
    saved_visual_outputs: dict[str, np.ndarray], 
    nrow: int, 
    ncol: int
)

但我收到一个错误,表明字典不可下标:

发生异常:TypeError “类型”对象不可下标 文件“functions.py”,第 168 行,在 saved_visual_outputs: dict[str, np.ndarray],

我的猜测是 dict 变量不能包含数组,但我没有看到为什么不包含的原因。

非常感谢任何关于原因的提示!

【问题讨论】:

    标签: numpy python-3.x


    【解决方案1】:

    要解决此错误,您需要使用 typing 模块中的 Dict 类,如下所示:

    from typing import Dict
    import numpy as np
    
    def processing_stitch_visual_outputs(
        saved_visual_outputs: Dict[str, np.ndarray], 
        nrow: int, 
        ncol: int
    )
    

    我自己没有尝试过,但是您使用类型提示的方式可能适用于 python 版本 3.9 及更高版本,因此尝试升级您的 python 版本也可能解决此问题。

    【讨论】:

    • 谢谢!!这样就完成了。
    猜你喜欢
    • 2017-05-27
    • 2020-09-12
    • 1970-01-01
    • 2011-05-30
    • 2022-07-01
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 2011-10-01
    相关资源
    最近更新 更多