问题描述
{
(int,int): {string: {string: int,string: float}}
}
我想知道如何使用类型提示来指定它。因此,这些位很清楚:
Dict[Tuple[int,int],Dict[str,# what comes here]]
但是,内部字典的两个键具有int
和float
值类型。我不确定该如何注释
解决方法
您应该可以使用Union
:
联盟类型;
Union[X,Y]
表示X或Y。
from typing import Union
Dict[Tuple[int,int],Dict[str,Union[int,float]]]
话虽如此,如果键始终相同,最好使用tuple
或namedtuple
代替内部dict
。