python中的reverse()用法? [英] reverse() usage in python?

查看:81
本文介绍了python中的reverse()用法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

sort() 和 reverse() 方法在排序或反转大列表时修改列表以节省空间.提醒您它们是通过副作用操作的,它们不会返回排序或反转的列表.

The sort() and reverse() methods modify the list in place for economy of space when sorting or reversing a large list. To remind you that they operate by side effect, they don’t return the sorted or reversed list.

以上文本可以在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 does "modify the list in place for economy of space" mean?

这意味着它不会创建列表的副本.

What this means is that it does not create a copy of the list.

为什么,x.reverse() 成功反转 x?

why does, x.reverse() Successfully 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()用法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆