Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 全局变量和局部变量之间的差异_Python_Arrays_Global Variables - Fatal编程技术网

Python 全局变量和局部变量之间的差异

Python 全局变量和局部变量之间的差异,python,arrays,global-variables,Python,Arrays,Global Variables,我是python新手,想知道返回数组时将空数组作为全局变量还是局部变量的区别 import collections def check_duplicates(data: list) -> list: dupes = [] for i in data: if data.count(i) > 1: dupes.append(i) return dupes if __name__ == "__main__": assert li

我是python新手,想知道返回数组时将空数组作为全局变量还是局部变量的区别

import collections

def check_duplicates(data: list) -> list:
    dupes = []

    for i in data:
        if data.count(i) > 1:
        dupes.append(i)
    return dupes

if __name__ == "__main__":
assert list(check_duplicates([1, 2, 3, 4, 5])) == [],

Your Result:[]
Right Result:[]


最初,我的数组在函数之外,无法返回空数组,我想知道

好吧,首先,原始代码的编写方式正确(但不是按照您希望的方式),一旦您为生成非空数组的列表运行它,它将停止提供空数组。剩下的唯一一件事就是理解它为什么会这样做

解释

首先,列表是可变的数据类型

a = [1,2] #create a list object and bind it to the name a
b = a #both b and a are now referring to the same list object
a.append(3) #the list object a was referring to has been changed inplace, or mutated.
#notice that coincidentally b was referring to this same object
print(a) #Prints: [1,2,3]
print(b) #Prints: [1,2,3]
这意味着,如果您使用列表的方法进行更改,例如
.append
,它将在原地更改列表对象,引用它的所有名称(例如
a
b
)都将反映更改

第二,您的原始代码,带有一些注释

dupes = [] #create an empty list, and have the name dupes refer to the list object

def check_duplicates(data: list) -> list:


    for i in data:
        if data.count(i) > 1:
            dupes.append(i) #mutate the list object that dupes was referring to.
    return dupes #return the list object dupes was referring to.
重要的是要认识到,在整个函数中,您永远不会重新分配名称
dups
,因此它会继续引用相同的列表对象。其结果是以下行为:

dupes = []

def check_duplicates(data: list) -> list:


    for i in data:
        if data.count(i) > 1:
            dupes.append(i)
    return dupes

check_duplicates([1])
print(dupes) #[]
check_duplicates([2])
print(dupes) #[]
check_duplicates([1, 1])
print(dupes) #[1, 1]
check_duplicates([2])
print(dupes) #[1, 1] !!!! The dupes list continues to refer to the object, and any further append calls just add to it
check_duplicates([99, 99, 100, 100])
print(dupes) #[1, 1, 99, 99, 100, 100]
check_duplicates([2])
print(dupes) #[1, 1, 99, 99, 100, 100]
希望这能让事情变得更清楚。
dupes
名称将继续引用函数中正在变异的列表,这就是为什么除非您将
dupes
重新分配给函数中的新空列表,否则您将继续获得旧结果。希望这有帮助。
.

您能出示您的“原始”代码吗?此外,您不需要指定参数的数据类型,也不需要在python中返回。只需将其编辑到答案中,不要将其作为comment@ParitoshSingh谢谢您的回复,我在上面添加了我的原始代码,请更正您的缩进。另外,“无法返回空数组”是什么意思?你有错误吗?意外结果?@Thierrylahuille抱歉,我如何更正缩进?
dupes = []

def check_duplicates(data: list) -> list:


    for i in data:
        if data.count(i) > 1:
            dupes.append(i)
    return dupes

check_duplicates([1])
print(dupes) #[]
check_duplicates([2])
print(dupes) #[]
check_duplicates([1, 1])
print(dupes) #[1, 1]
check_duplicates([2])
print(dupes) #[1, 1] !!!! The dupes list continues to refer to the object, and any further append calls just add to it
check_duplicates([99, 99, 100, 100])
print(dupes) #[1, 1, 99, 99, 100, 100]
check_duplicates([2])
print(dupes) #[1, 1, 99, 99, 100, 100]