我对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}$
append
没有得到两个参数,而是得到了一个tuple
,正如您在教程tuple中看到的那样,len(word)
和{t
是一个元组列表。在相关问题 更多 >
编程相关推荐