Python:为什么我的列表在我没有实际更改的情况下会更改? [英] Python: why does my list change when I'm not actually changing it?

查看:28
本文介绍了Python:为什么我的列表在我没有实际更改的情况下会更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新手有问题,请温柔:

list = [1, 2, 3, 4, 5]列表 2 = 列表def fxn(list,list2):对于列表中的数字:打印(编号)打印(列表)list2.remove(number)打印(删除列表后是,列表,"和列表2是,列表2)返回列表,列表 2列表,列表2 = fxn(列表,列表2)print("fxn list 之后是", list)print("fxn list2 之后是", list2)

这导致:

1[1, 2, 3, 4, 5]在删除列表是 [2, 3, 4, 5] 和列表 2 之后是 [2, 3, 4, 5]3[2, 3, 4, 5]在删除列表是 [2, 4, 5] 并且列表 2 是 [2, 4, 5] 之后5[2, 4, 5]在删除列表是 [2, 4] 并且列表 2 是 [2, 4] 之后在 fxn 列表是 [2, 4] 之后在 fxn list2 是 [2, 4] 之后

我不明白为什么当我只做 list2.remove() 而不是 list.remove() 时列表会改变.我什至不确定使用什么搜索词来弄清楚.

解决方案

那是因为 listlist2 在你完成任务后引用了同一个列表 list2=list.

试试这个,看看它们是指相同的对象还是不同的:

id(list)编号(列表 2)

示例:

<预><代码>>>>列表 = [1, 2, 3, 4, 5]>>>列表 2 = 列表>>>编号(列表)140496700844944>>>编号(列表 2)140496700844944>>>list.remove(3)>>>列表[1, 2, 4, 5]>>>列表2[1, 2, 4, 5]

如果您真的想创建 list 的副本,以便 list2 不引用原始列表而是引用列表的副本,请使用切片运算符:

list2 = list[:]

示例:

<预><代码>>>>列表[1, 2, 4, 5]>>>列表2[1, 2, 4, 5]>>>列表 = [1, 2, 3, 4, 5]>>>列表2 = 列表[:]>>>编号(列表)140496701034792>>>编号(列表 2)140496701034864>>>list.remove(3)>>>列表[1, 2, 4, 5]>>>列表2[1, 2, 3, 4, 5]

另外,不要使用list作为变量名,因为原本list是指类型列表,而是通过定义自己的list 变量,您隐藏了引用类型列表的原始 list.示例:

<预><代码>>>>列表<输入列表">>>>类型(列表)<输入'类型'>>>>列表 = [1, 2, 3, 4, 5]>>>列表[1, 2, 3, 4, 5]>>>类型(列表)<输入列表">

Newbie with a question, so please be gentle:

list = [1, 2, 3, 4, 5]
list2 = list

def fxn(list,list2):
    for number in list:
        print(number)
        print(list)
        list2.remove(number)
        print("after remove list is  ", list, " and list 2 is  ", list2)
    return list, list2

list, list2 = fxn(list, list2)
print("after fxn list is  ", list)
print("after fxn list2 is  ", list2)

This results in:

1
[1, 2, 3, 4, 5]
after remove list is   [2, 3, 4, 5]  and list 2 is   [2, 3, 4, 5]
3
[2, 3, 4, 5]
after remove list is   [2, 4, 5]  and list 2 is   [2, 4, 5]
5
[2, 4, 5]
after remove list is   [2, 4]  and list 2 is   [2, 4]
after fxn list is   [2, 4]
after fxn list2 is   [2, 4]

I don't understand why list is changing when I am only doing list2.remove(), not list.remove(). I'm not even sure what search terms to use to figure it out.

解决方案

That's because both list and list2 are referring to the same list after you did the assignment list2=list.

Try this to see if they are referring to the same objects or different:

id(list)
id(list2)

An example:

>>> list = [1, 2, 3, 4, 5]
>>> list2 = list
>>> id(list)
140496700844944
>>> id(list2)
140496700844944
>>> list.remove(3)
>>> list
[1, 2, 4, 5]
>>> list2
[1, 2, 4, 5]

If you really want to create a duplicate copy of list such that list2 doesn't refer to the original list but a copy of the list, use the slice operator:

list2 = list[:]

An example:

>>> list
[1, 2, 4, 5]
>>> list2
[1, 2, 4, 5]
>>> list = [1, 2, 3, 4, 5]
>>> list2 = list[:]
>>> id(list)
140496701034792
>>> id(list2)
140496701034864
>>> list.remove(3)
>>> list
[1, 2, 4, 5]
>>> list2
[1, 2, 3, 4, 5]

Also, don't use list as a variable name, because originally, list refers to the type list, but by defining your own list variable, you are hiding the original list that refers to the type list. Example:

>>> list
<type 'list'>
>>> type(list)
<type 'type'>
>>> list = [1, 2, 3, 4, 5]
>>> list
[1, 2, 3, 4, 5]
>>> type(list)
<type 'list'>

这篇关于Python:为什么我的列表在我没有实际更改的情况下会更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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