python - Python中set[...]的含义是什么
问题描述
我对编程和 python 很陌生。
据我所知,对于 set 函数的初始化,您使用set()
一个空集,并将{ ... }
元素初始化为一个集合。在示例代码中,我看到了set[X]
. 集合中使用的方括号是什么意思?
这是示例代码:
def example_function(x: set[A], y: set[B]) -> set[tuple[A, B]]:
res = set()
for i in x:
for j in y:
res.add((i, j))
return res
解决方案
它是一个类型提示,在这个例子中,我们使用类型提示来进行集合,集合项的类型在括号中。
例子:
# Python 3.9
# For collections, the type of the collection item is in brackets
int_list: list[int] = [1]
int_set: set[int] = {6, 7}
# Python 3.8 and earlier, the name of the collection type is
# capitalized, and the type is imported from 'typing'
from typing import List, Set, Dict, Tuple, Optional
int_list: List[int] = [1]
int_set: Set[int] = {6, 7}
推荐阅读
- android - 如何在应用小部件中使用 ListView 并在 android 中填充数据?
- r - R中的逐行频率表
- python - tkinter 应用程序可以使用 httpServer 吗?
- autodesk-forge - 我们如何实现 removeAllProperties() 查看器 API?
- sql-server - SQL 查询在 VBA 中不起作用。抛出自动化错误
- excel - 在 Excel 的堆栈柱形图中显示数据
- r - rply2 运行 32 位 r 而不是 64 位
- swift - 错误:两个演绎版的密钥相同(Xcode 10.1、Mojave 10.14.1)
- rhel - Chrony 在 10 分钟后失去与本地主机的连接
- visual-studio - 如何指定 VSPackage / VSIX Manifest prereq 或安装目标版本范围?