python中的list1 = [] list2 = []和list1 = list2 = []有什么区别? [英] What's the difference between list1 = [] list2 = [] and list1 = list2 = [] in python?

查看:128
本文介绍了python中的list1 = [] list2 = []和list1 = list2 = []有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用python,并且尝试使用列表推导来初始化两个列表.像这样

list1 = list2 = [0.0] * 57

当我这样做并插入带有值的这些列表时,与分别初始化这些列表时得到的值相比,我得到了一组不同的值(不正确的值).喜欢

list1 = [0.0] * 57
list2 = [0.0] * 57

第一种情况是怎么回事?为什么在这两种情况下我得到不同的答案?

解决方案

在以下情况下,您将创建一个列表,并使2个变量指向该列表;即2个引用相同的对象:

list1 = list2 = [123] * 3
list1.append(456)

print list1 =>  # prints [123, 123, 123, 456]
print list2 =>  # prints [123, 123, 123, 456]
print list1 is list2   # prints True

这将创建2个新列表,并将其中一个分配给list1,将另一个分配给list2:

list1 = [123] * 3
list2 = [123] * 3
# or  list1, list2 = [123] * 3, [123] * 3

list1.append(456)

print list1  # prints [123, 123, 123, 456]
print list2  # prints [123, 123, 123]
print list1 is list 2  # prints False

这与是否通过引用复制或存储值有关.对于整数和字符串之类的不可变对象,这无关紧要:

# a and b contain the same int object
# but it's OK because int's are immutable
a = b = 1
a += 2  # creates a new int from 1+2 and assigns it to `a`
print b  # => 1  ... b is unchanged
print a  # => 3

换句话说,int s(也不是float s或str s等)没有方法可以更改您在其上调用该方法的值.相反,它们都返回该类型的新实例;所以-5返回一个新的int -5而不是已修改的现有int 5;同样,a += 2等效于a = a + 2,其中a + 2(即在a上调用的方法__add__)返回一个新的int,其值是a + 2,并且对该引用的引用被分配回了a. /p>

I just started using python and I am trying to initialize two lists using list comprehensions. Like this

list1 = list2 = [0.0] * 57

When i do this and insert these lists with values i am getting a different set of values (incorrect values) when compared to the values i get when i initialize these lists seperately. Like

list1 = [0.0] * 57
list2 = [0.0] * 57

What is happening in the first case ? Why am I getting different answers for these 2 cases ?

解决方案

In the following case, you are creating one list and making 2 variables point to it; i.e. 2 references to the same object:

list1 = list2 = [123] * 3
list1.append(456)

print list1 =>  # prints [123, 123, 123, 456]
print list2 =>  # prints [123, 123, 123, 456]
print list1 is list2   # prints True

whereas this creates 2 new lists and assigns one to list1 and the other to list2:

list1 = [123] * 3
list2 = [123] * 3
# or  list1, list2 = [123] * 3, [123] * 3

list1.append(456)

print list1  # prints [123, 123, 123, 456]
print list2  # prints [123, 123, 123]
print list1 is list 2  # prints False

This has to do with whether values are copied or stored in variables by reference. In the case of immutable objects such as integers and strings, this doesn't matter:

# a and b contain the same int object
# but it's OK because int's are immutable
a = b = 1
a += 2  # creates a new int from 1+2 and assigns it to `a`
print b  # => 1  ... b is unchanged
print a  # => 3

In other words, ints (nor floats nor strs etc) have no methods that change the value you're calling the method on; instead, they all return new instances of that type; so -5 returns a new int -5 not the existing int 5 modified; also, a += 2 is equivalent to a = a + 2 where a + 2 (i.e the method __add__ called on a) returns a fresh int whose value is a + 2 and a reference to that gets assigned back to a.

这篇关于python中的list1 = [] list2 = []和list1 = list2 = []有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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