Python“放置在"功能 [英] Python "IN PLACE" functions
问题描述
Python中的某些功能(例如[].sort
和[].reverse
)在"IN PLACE"中运行而其他某些功能(例如[].append
)不运行的特殊原因是什么?
What's the particular reason that some functions in Python operate "IN PLACE", like [].sort
and [].reverse
, while some others like [].append
not?
推荐答案
根据我的Python编程第4版:
According to my Programming Python 4th edition:
默认情况下,pop等同于 获取,然后删除最后一个 偏移量为-1的项目.有一个论点, pop删除并在以下位置返回该项目 该偏移量–list.pop(-1)相同 作为list.pop().对于就地更改 诸如追加,插入,删除, 并弹出,不会在其中创建新列表 内存,因此执行速度很快 (性能可能进一步取决于 哪一端是顶端",但这在 转向取决于Python的当前列表 实施以及衡量 概念,我们将在后面进行探讨.)
By default, pop is equivalent to fetching, and then deleting, the last item at offset −1. With an argument, pop deletes and returns the item at that offset—list.pop(-1) is the same as list.pop(). For in-place change operations like append, insert, del, and pop, no new list is created in memory, so execution is quick (performance may further de- pend upon which end is the "top," but this in turn depends on Python’s current list implementation, as well as measurement concepts we’ll explore later).
实际上有一整节专门讨论此问题,但这几乎可以回答您的问题.
There is actually a whole section devoted to this, but this pretty much answers your question.
这篇关于Python“放置在"功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!