python在使用嵌套for语句中,为什么遍历的时候会有一些元素没有遍历到?

一个遍历寻取出列表中两个元素,后将第一个元素放置到第二个元素前面的搜索算法邻域变换的设计。但是我用赋值的方法将列表内容赋值给initial_2,并把改动后的列表用initial_plan来表示时,明明每次遍历后都会用initial_plan=initial_2的方法重置initial_plan为什么遍历的时候,intial_2会发生变化。

而当我直接使用列表,而不是赋值变量时这个情况就不会发生。

initial=['M0','M7','M6','M3','M8','M0']
del initial[-1]
del initial[0]
initial_2=initial#消除回路和M0后的
def first_way():#第一种邻域变换
    
    for i in initial:
        initial_plan=initial_2
        for j in initial:
            initial_plan=initial_2
            print(i,j)
            #消除邻域变换带来的影响
            if i!=j:  
                first_choice=initial.index(i)#1
                second_choice=initial.index(j)#0
                if first_choice>second_choice:
                    del initial_plan[first_choice]
                    initial_plan.insert(second_choice,i)
                    print(initial_plan)
                elif first_choice<second_choice:
                    del initial_plan[first_choice]#M7,M6
                    initial_plan.insert(second_choice-1,i)
                    print(initial_plan)
            else:
                print('两者选到了一样的算子')
first_way()

得到的结果是这样的,不是我需要的结果

M7 M7
两者选到了一样的算子    
M7 M6
['M7', 'M6', 'M3', 'M8']
M7 M3
['M6', 'M7', 'M3', 'M8']
M7 M8
['M6', 'M3', 'M7', 'M8']
M3 M6
['M3', 'M6', 'M7', 'M8']
M3 M6
['M3', 'M6', 'M7', 'M8']
M3 M7
['M6', 'M3', 'M7', 'M8']
M3 M8
['M6', 'M7', 'M3', 'M8']
M3 M6
['M3', 'M6', 'M7', 'M8']
M3 M6
['M3', 'M6', 'M7', 'M8']
M3 M7
['M6', 'M3', 'M7', 'M8']
M3 M8
['M6', 'M7', 'M3', 'M8']
M8 M6
['M8', 'M6', 'M7', 'M3']
M8 M6
['M8', 'M6', 'M7', 'M3']
M8 M7
['M6', 'M8', 'M7', 'M3']
M8 M3
['M6', 'M7', 'M8', 'M3']

当我直接使用列表形式时,就是我需要的结果

initial=['M0','M7','M6','M3','M8','M0']
del initial[-1]
del initial[0]
initial_=initial#消除回路和M0后的
def first_way():#第一种邻域变换
    
    for i in initial:
        initial_plan=['M7','M6','M3','M8','M1']
        for j in initial:
            initial_plan=['M7','M6','M3','M8','M1']
            
            print(i,j)
            #消除邻域变换带来的影响
            if i!=j:  
                first_choice=initial.index(i)#1
                second_choice=initial.index(j)#0
                
                if first_choice>second_choice:
                    del initial_plan[first_choice]
                    initial_plan.insert(second_choice,i)
                    print(initial_plan)
                        
                elif first_choice<second_choice:
                    del initial_plan[first_choice]#M7,M6
                    initial_plan.insert(second_choice-1,i)
                    print(initial_plan)
                    
            else:
                print('两者选到了一样的算子')
first_way()

得到结果如下

M7 M7
两者选到了一样的算子
M7 M6
['M7', 'M6', 'M3', 'M8', 'M1']
M7 M3
['M6', 'M7', 'M3', 'M8', 'M1']
M7 M8
['M6', 'M3', 'M7', 'M8', 'M1']
M6 M7
['M6', 'M7', 'M3', 'M8', 'M1']
M6 M6
两者选到了一样的算子
M6 M3
['M7', 'M6', 'M3', 'M8', 'M1']
M6 M8
['M7', 'M3', 'M6', 'M8', 'M1']
M3 M7
['M3', 'M7', 'M6', 'M8', 'M1']
M3 M6
['M7', 'M3', 'M6', 'M8', 'M1']
M3 M3
两者选到了一样的算子
M3 M8
['M7', 'M6', 'M3', 'M8', 'M1']
M8 M7
['M8', 'M7', 'M6', 'M3', 'M1']
M8 M6
['M7', 'M8', 'M6', 'M3', 'M1']
M8 M3
['M7', 'M6', 'M8', 'M3', 'M1']
M8 M8
两者选到了一样的算子

在做毕业设计,碰到了困难。可能表达的不好,毕竟不是学代码专业的,请求各位大神解答。

        initial_plan=initial_2.copy()

http://www.manongjc.com/detail/11-fxixmbzipppbzwh.html

您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632