Python全局变量矛盾

2024-04-24 08:23:57 发布

您现在位置:Python中文网/ 问答频道 /正文

由于某种原因,主函数中的变量“a”在函数调用后会发生变化。尽管“a”在函数中甚至没有变化。尽管这样做了,但这样的改变不应该影响它在函数之外的值,对吗?你知道吗

我使用的是python3.6,我不知道为什么会发生这种情况。任何帮助都将不胜感激

def mhmArgSort(myNumListInput):
    myNumList=myNumListInput
    cnt=0
    endCnt=len(myNumList)
    NumListSortedIndices=[]
    for cnt1 in range(len(myNumList)):
        smallestNum=100000000000
        smallestNumIndex=0
        for cnt in range(len(myNumList)):
            if myNumList[cnt]<smallestNum:
                smallestNum=myNumList[cnt]
                smallestNumIndex=cnt
        NumListSortedIndices.append(smallestNumIndex)
        myNumList[smallestNumIndex]=100000000000
    return NumListSortedIndices

a=[1,2,3,4,-1,0,3]
print(a)
b=mhmArgSort(a)
print(a)
print(b)


This produces the following result:
[1, 2, 3, 4, -1, 0, 3]
[100000000000, 100000000000, 100000000000, 100000000000, 100000000000, 100000000000, 100000000000]
[4, 5, 0, 1, 2, 6, 3]

Tags: 函数inforlen情况rangeprint函数调用