输入键值对列表提示?

问题描述 投票:0回答:1

我对 python 和使用 python 创建类型相当陌生。仍然让我头疼不已。

我正在尝试想出一种使用类型定义键值对列表的方法。

我所说的结构是:

[{'key1':'value1'},{'key2','value2'},...]

我能想到的最好的办法是:

List[Dict[str,str]]

但我确信这是错误的。

python python-typing
1个回答
0
投票

正如评论中提到的,你的方法没有任何问题。

或者,为了说明可能性,您还可以创建自定义项目类型注释:

from typing import Dict, List, TypeVar


Item: TypeVar = Dict[str, str]
data: List[Item] = [{'key1':'value1'},{'key2','value2'},...]
© www.soinside.com 2019 - 2024. All rights reserved.