Python-在while循环期间将列表追加到列表-结果与预期不符 [英] Python - Appending list to list during while loop - Result not as expected

查看:433
本文介绍了Python-在while循环期间将列表追加到列表-结果与预期不符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处使用Python/编程新手,试图弄清楚while循环的含义.首先输入代码:

Python/programming newbie here, trying to figure out what is going in with this while loop. First the code:

var_list = []
split_string = "pink penguins,green shirts,blue jeans,fried tasty chicken,old-style boots"

def create_variations(split_string):
    init_list = split_string.split(',')
    first_element = init_list[0]

    # change first element of list to prepare for while loop iterations
    popped = init_list.pop()
    added = init_list.insert(0, popped)

    while init_list[0] != first_element:
        popped = init_list.pop()
        added = init_list.insert(0, popped)
        print init_list # prints as expected, with popped element inserted to index[0] on each iteration
        var_list.append(init_list) # keeps appending the same 'init_list' as defined on line 5, not those altered in the loop!

    print var_list

create_variations(split_string)

我的目标是创建init_list的所有变体,这意味着索引被轮换,因此每个索引只能成为第一个索引.然后,将这些变体附加到另一个列表,该列表为此代码中的var_list.

My objective is to create all variations of the init_list, meaning that the indexes are rotated so that each index gets to be first just once. These variations are then appended to another list, which is the var_list in this code.

但是,我没有从while循环中获得预期的结果.在while循环中,代码段print init_list实际上打印了我想要的变体.但是代码var_list.append(init_list)的下一行未附加这些变体.而是将第5行中创建的init_list重复附加到var_list.

However, I am not getting the result I am expecting from the while loop. In the while loop, the piece of code print init_list actually prints the variations I want; but the next line of code var_list.append(init_list) does not append these variations. Instead the init_list as created on line 5 is repeatedly appended to the var_list.

这是怎么回事?以及如何获取在while循环中创建的init_list的不同变体形式,以追加到var_list.

What is going on here? And how can I get the different variations of the init_list created in the while loop to append to the var_list.

我希望用于var_list的输出:

[['fried tasty chicken', 'old-style boots', 'pink penguins', 'green shirts', 'blue jeans'],
 ['blue jeans', 'fried tasty chicken', 'old-style boots', 'pink penguins', 'green shirts'],
 ['green shirts', 'blue jeans', 'fried tasty chicken', 'old-style boots', 'pink penguins'],
 ['pink penguins', 'green shirts', 'blue jeans', 'fried tasty chicken', 'old-style boots']]

推荐答案

此行

var_list.append(init_list)

您每次都添加对init_list的引用.但是您需要创建一个新列表.使用这个

you are adding a reference to init_list everytime. But you need to create a new list. Use this

var_list.append(init_list[:])

说明

打印init_list时,它会打印当前状态.将其添加到var_list时,没有添加当前状态.您正在添加参考.因此,当实际列表更改时,所有引用都指向同一数据.

When you are printing init_list, it prints the current state. When you are adding it to var_list, you are not adding the current state. You are adding a reference. So, when the actual list changes all references point to the same data.

您可以像这样简化程序

def create_variations(split_string):
    init_list = split_string.split(', ')
    for i in range(len(init_list)):
        var_list.append(init_list[:])
        init_list.insert(0, init_list.pop())

    print var_list

这篇关于Python-在while循环期间将列表追加到列表-结果与预期不符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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