问题描述
sort() 和 reverse() 方法在排序或反转大列表时修改列表以节省空间.提醒您它们是通过副作用操作的,它们不会返回排序或反转的列表.
以上文本可以在http://docs.python.org/2/library/stdtypes.html#mutable-sequence-types
为节省空间而修改清单"是什么意思?
What does "modify the list in place for economy of space" mean?
示例:
x = ["happy", "sad"]
y = x.reverse()
会将 None
返回到 y
.那么为什么,
would return None
to y
. So then why does,
x.reverse()
成功反转x
?
推荐答案
这意味着它不会创建列表的副本.
What this means is that it does not create a copy of the list.
为什么,x.reverse() 成功反转 x?
我不明白这个问题.它这样做是因为这就是它的设计目的(反转 x
并返回 None
).
I don't understand this question. It does this because that's what it's designed to do (reverse x
and return None
).
请注意,sort()
和 reverse()
的对应项不会修改原始文件并返回副本.这些函数被称为 sorted()
和reversed()
:
Note that sort()
and reverse()
have counterparts that don't modify the original and return a copy. These functions are called sorted()
and reversed()
:
In [7]: x = [1, 2, 3]
In [8]: y = list(reversed(x))
In [9]: x
Out[9]: [1, 2, 3]
In [10]: y
Out[10]: [3, 2, 1]
这篇关于python中的reverse()用法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!