如何将列表添加到另一个列表

时间:2019-09-23 07:21:38

标签: python list function append extend

我有一个名为“ organizer”的函数,该函数使用一个列表和一个索引(数字)两个值来重新组织此列表,然后返回一个新列表。

我想执行此功能10次以创建10个列表并将此列表添加到单个列表中以供以后使用,程序会完成此操作,但是它会重复最后一个列表10次,但不会添加其他列表

我将函数“ organizer”放在这里,因为我无法找到问题所在,因此可能需要更多信息。 我一直在沿着函数使用print函数来查看失败的地方,并按我的意愿创建列表,问题是创建完列表后,只要循环执行,它就将最后创建的列表复制多次。它会产生最终清单并复制几次

代码如下:

number_list = ["12","11","10","9","8","7","6","5","4","3","2","1"]    
indexes = [0,5,6,8,10,4,5,2,1,9]    

def organizer(list,index): # Takes a list and reorganize it by a number which is an index.    
    number1 = index - 1    
    count_to_add = -1    
    list_to_add = []    
    for item in range(number1):    
        count_to_add += 1    
        a = list[count_to_add]    
        list_to_add.append(a)    

    number2 = index - 1    
    for item1 in range(number2):    
        del list[0]    

    list.extend(list_to_add)    
    return list    


def lists_creator(list_indexes):  # Create a list of 10 lists ,with 12 elements each.    
    final_list = []
    count = -1

    for item in list_indexes:
        count += 1
        result = organizer(number_list, list_indexes[count])
        final_list.append(result)

    return final_list

final_output = lists_creator(indexes)
print(final_output)

结果是:(相同列表十次)

[['7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8'], ['7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8'], ['7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8'], ['7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8'], ['7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8'], ['7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8'], ['7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8'], ['7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8'], ['7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8'], ['7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8']]

注意:如果我在list_creator函数中更改第28行

final_list.append(result)

final_list.extend(result)

结果是:

['12', '11', '10', '9', '8', '7', '6', '5', '4', '3', '2', '1', '8', '7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '3', '2', '1', '12', '11', '10', '9', '8', '7', '6', '5', '4', '8', '7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '11', '10', '9', '8', '7', '6', '5', '4', '3', '2', '1', '12', '8', '7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '4', '3', '2', '1', '12', '11', '10', '9', '8', '7', '6', '5', '3', '2', '1', '12', '11', '10', '9', '8', '7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8', '7', '6', '5', '4', '7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8']

这是我想要的结果,但不是一组列表。

2 个答案:

答案 0 :(得分:1)

您每次都使用相同的列表,因此结果实际上由一个列表组成,

在您的list_creator方法中,您必须给organizer方法一个原始列表number_list的副本,否则,您将一遍又一遍地重复同一列表:

def lists_creator(list_indexes):  # Create a list of 10 lists ,with 12 elements each.    
    final_list = []
    count = -1

    for item in list_indexes:
        count += 1
        result = organizer(list(number_list), list_indexes[count])
        final_list.append(result)

    return final_list

答案 1 :(得分:1)

def lists_creator(list_indexes):  # Create a list of 10 lists ,with 12 elements each.
    final_list = []
    list = []

    for i in range(len(list_indexes)):
        result = organizer(number_list, list_indexes[i])
        list.extend(result)

    n = 0
    m = 12
    for n_times in range(len(list_indexes)):
        final_list.append(list[n:m])
        n = m
        m += 12

    return final_list