Python3为什么这里的append()可以接受两个参数?

2024-04-19 09:42:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python3不太熟悉。当我学习元组时,我在书中找到了这个代码。在

txt = 'but sort what light in yonder window breaks'
words = txt.split()
t = list()
for word in words:
    t.append((len(word),word))

t.sort(reverse = True)

res = list()

for length, word in t:
    res.append(word)

print(res)

我对这个代码有两个问题。首先这本书说:

The first loop builds a list of tuples, where each tuple is a word preceded by its length.

第一个循环是如何创建元组列表的?从教程中,我了解到元组的创建如下所示。在

^{pr2}$

第二个问题是为什么append()可以接受两个参数?在

^{3}$

Tags: 代码intxtforressortwhatlength