简体   繁体   English

list [::]和list有什么区别?

[英]What's the difference between list[::] and list?

This is a question rotating a matrix 90 degrees clockwise, i don't understand why i cannot use: 这是一个将矩阵顺时针旋转90度的问题,我不明白为什么我不能使用:

matrix = zip(*matrix[::-1])

but: 但:

class Solution:
    def rotate(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: void Do not return anything, modify matrix in-place instead.
        """
        matrix[::] = zip(*matrix[::-1])

matrix in your method is a reference to a matrix object. matrix中的方法是将基体对象的引用。 Assignment to matrix will change matrix to reference your newly created object, but not change the contents of the original object. 分配给matrix会更改matrix以引用您新创建的对象,但不会更改原始对象的内容。 matrix[::] = invokes __setitem__ on the object referenced by matrix which changes the contents of the object accordingly. matrix[::] =matrix引用的对象上调用__setitem__ ,该对象相应地更改了对象的内容。

In Python, all assignments bind a reference to a name. 在Python中,所有分配都将引用绑定到名称。 Operators call a method of an existing reference 1 . 操作员调用现有参考文献1的方​​法 In your case, the statement 就您而言,声明

matrix = ...

is purely an assignment 2 . 纯粹是一项任务2 It computes the right hand side, and binds it to the name matrix in the local function scope. 它计算右侧,并将其绑定到局部函数范围内的名称matrix Whatever object matrix referred to when you passed it in remains untouched. 传递对象时引用的任何对象matrix保持不变。

This is why you don't see the changes you made. 这就是为什么您看不到所做更改的原因。 It's not that the function doesn't work this way, it's that it doesn't do anything with the rotated list. 并不是说该功能不能以这种方式工作,而是对旋转后的列表没有任何作用。 The data is discarded as soon as the function exits. 该函数退出后,数据将立即丢弃。

The operation 手术

matrix[:] = ...

on the other hand is not an assignment in the semantic sense, despite the = symbol 3 . 另一方面,尽管=符号3 ,但在语义上不是赋值。 It's a call to matrix.__setitem__(...) 4 . 这是对matrix.__setitem__(...) 4的调用。 The __setitem__ method, like any other method, operates directly on the object without changing it's name bindings. 与其他任何方法一样, __setitem__方法可直接在对象上操作,而无需更改其名称绑定。

As far as indexing goes, [:] is equivalent to [::] . 就索引而言, [:]等效于[::] They are shorthand for [0:len(matrix)] and [0:len(matrix):1] , respectively. 它们分别是[0:len(matrix)][0:len(matrix):1]简写。 In both cases, the default step size will be used. 在这两种情况下,都将使用默认步长。 In general, any index with colons in it will be converted to a slice object. 通常,任何包含冒号的索引都将转换为slice对象。 Missing elements are set to None and replaced by the sequence-specific defaults shown here. 缺少的元素设置为“ None并替换为此处显示的特定于序列的默认值。


1 Some operators, like += perform an assignment after calling a method. 1一些运算符,例如+= ,在调用方法后执行赋值。 These are called augmented assignments . 这些被称为扩充作业 But that's not a case we're interested in right now. 但这不是我们现在感兴趣的情况。

2 Besides literal assignment statements ( = ), some other types of assignments are def (which binds a function object to its name), class (which does the same for a class object), import (which binds a module or element of a module to a name), passing arguments to a function (which binds objects to the local argument names or kwarg dictionary keys), and for (which binds an element from an iterator to the loop variable at each iteration). 2除了文字赋值语句= )外,其他一些类型的赋值是def (将功能对象绑定到其名称), class (对类对象执行相同的操作), import (将模块或模块元素绑定)名称),将参数传递给函数(将对象绑定到局部参数名称或kwarg字典键)和for (将元素从迭代器绑定到循环变量)。

3 It's still an assignment from the point of view of the parser, but the statement is handled completely differently. 3从解析器的角度来看,这仍然是一个任务 ,但是对语句的处理完全不同。 A similar statement that is not actually an assignment is using the = operator on an attribute implemented as a descriptor, such as a property . 实际上不是赋值的类似语句是在实现为描述符的属性(例如property上使用=运算符。

4 Technically, it's more of an equivalent to type(matrix).__setitem__(matrix, ...) , but with some additional optimizations. 4从技术上讲,它更等效于type(matrix).__setitem__(matrix, ...) ,但还有一些其他优化。 For example, the metaclass of type(matrix) won't ever be searched. 例如,将永远不会搜索type(matrix)的元类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM