简体   繁体   English

整数和对python列表切片的引用

[英]Integer and referencing on python list slicing

Could anybody explain the following list reference issue? 谁能解释以下列表参考问题? I haven't understood it clearly . 我不清楚。

For example : 例如 :

>>> a = [1,2,3,4]
>>> a[0:2] = 11,22
>>> a
[11, 22, 3, 4]
>>> b = a[0:2]
>>> b[0:2] = 33,44
>>> b
[33, 44]
>>> a
[11, 22, 3, 4]

Why does a[0:2] = 11,22 changes the list a but b[0:2] = 33,44 doesn't? 为什么a[0:2] = 11,22更改列表ab[0:2] = 33,44没有更改列表? Isn't b referencing a[0:2] ? b不是引用a[0:2]吗? Why list's reference is changing its value, but integer's reference it won't? 为什么列表的引用会更改其值,但整数引用却不会更改?

Example: 例:

>>> a = 1
>>> b = a
>>> a = 2
>>> b
1

I think it is self-explanatory. 我认为这是不言而喻的。

>>> a = [1,2,3,4]  
>>> a[0:2] = 11,22  
>>> a  
[11, 22, 3, 4]    

You are modifying the value at the indices 0 and 1 of a. 您正在a的索引01处修改值。 So, original values at indices 2 and 3 remain unchanged. 因此,索引23原始值保持不变。

>>> b = a[0:2]  
>>> b[0:2] = 33,44  
>>> b  
[33, 44]  
>>> a  
[11, 22, 3, 4]  

You have copied the values at indices 0 and 1 from a to b and hence b has only 2 values. 您已将索引01处的值从a复制到b ,因此b只有2个值。 Then you are overwriting the values of b with [33,44] . 然后,用[33,44]覆盖b的值。 Hence the results what you observe. 因此,您所观察到的结果。

There are certain data types which are immutable (values cannot be changed) such as numbers, strings, tuple. 有些数据类型是不可变的(值不能更改),例如数字,字符串,元组。 Data types like dict, list are mutable (values can be changed) dict,list等数据类型是可变的(值可以更改)

Illustration 插图

>>> a = [1,2,3,4]
>>> b = a
>>> id(a)
46734952
>>> id(b)
46734952  

>>> b[1] = 8
>>> print(b)
[1, 8, 3, 4]
>>> print(a)
[1, 8, 3, 4]  

In the above case, b is a reference to a . 在上述情况下, b是一个参考a ie, Both a and b is pointing to the same list. 即, ab都指向同一列表。 Any change made to b will affect a also. b所做的任何更改也会影响a This can be verified by using the id function. 可以使用id函数进行验证。 Same id value says that both point to the same object. 相同的id值表示两者都指向同一对象。

>>> c = a[:]
>>> print(c)
[1, 8, 3, 4]
>>> id(a)
46734952
>>> id(c)
46563240  

In the above case, slice of a is assigned to c . 在上述情况下,将a切片分配给c Hence a copy is made and assigned to c . 因此,将制作一个副本并将其分配给c Hence the id value of a and c are different. 因此, acid值是不同的。

Because there are two types of data 因为有两种类型的数据

  1. mutable data types 可变数据类型

    • list 清单
    • dict 字典
    • set
    • classes
  2. immutable data types 不变的数据类型

    • numbers (boolean, integer, float and complex numbers) 数字(布尔,整数,浮点数和复数)
    • string
    • tuple 元组
    • frozenset 冰封

When you change mutable type you change it object and when you change immutable type you create new object and then assign it to variable 更改mutable type将更改它的对象;当更改immutable type时,将创建新的对象,然后将其分配给变量

I'll answer step by step: 我将逐步回答:

a[0:2] = 11,22

Is the same as doing a[0] = 11; a[1] = 22 与执行a[0] = 11; a[1] = 22 a[0] = 11; a[1] = 22 . a[0] = 11; a[1] = 22 That's regular assignment, no surprises here. 这是常规任务,在这里不足为奇。 When you do: 当您这样做时:

b = a[0:2]

You get a shallow copy of the list. 您会得到列表的浅表副本 This means that you get a new list, with a reference to each object. 这意味着您将获得一个新列表,其中包含对每个对象的引用。 Since in your case the list is filled with int s, it's essentially a copy as well. 由于在您的情况下,列表中填充有int ,因此它实际上也是一个副本。 But consider this example: 但是,请考虑以下示例:

Let's define a class, which wraps an int (or any other data type, it's python :) ), but is mutable. 让我们定义一个类,它包装一个int (或任何其他数据类型,它是python :)),但是是可变的。

In [41]: class num(object):
    ...:     def __init__(self, x):
    ...:         self.x = x
    ...:         

Now we'll define a as before, only with our class 现在,仅在我们的课程中定义a

In [42]: a = [num(x) for x in range(1,5)]

In [43]: a[0].x
Out[43]: 1

In [44]: a[1].x
Out[44]: 2

Now, we'll assign a slice of a into b . 现在,我们将指定的片ab Contrary to your example, when the list held the values of the numbers, here it hold the reference, so you could say they are the same: 与您的示例相反,当列表包含数字值时,此处包含引用,因此可以说它们是相同的:

In [45]: b = a[0:2]

In [46]: b[0].x
Out[46]: 1

In [47]: b[1].x
Out[47]: 2

So when I change one in b : 因此,当我在b更改一个时:

In [48]: b[0].x = 3

It's reflected in a : 它反映了a

In [50]: a[0].x
Out[50]: 3

But it's a different list. 但这是一个不同的列表。 So assigning a new instance to a place in the list... 因此,将新实例分配给列表中的位置...

In [49]: b[1] = num(20)

... is not reflected in a : ...是不是体现在a

In [51]: a[1].x
Out[51]: 2

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

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