Skip to main content
 首页 » 编程设计

python之函数调用后的方括号

2025年05月04日17webabcd

我是 Python 的新手,遇到了一段让我感到困惑的代码。

ts, pkt2 = capPort2.wait(1, 45)[0] 

前一行让我很困惑。我理解使用两个参数调用函数 wait,但是 [0] 是什么意思或做什么?

请您参考如下方法:

表示提取函数返回的列表/元组中的第一项。

In [1]: "this is a long sentence".split() 
Out[1]: ['this', 'is', 'a', 'long', 'sentence'] 
 
In [2]: "this is a long sentence".split()[0] 
Out[2]: 'this'