【问题标题】:How to add a list to another list如何将一个列表添加到另一个列表
【发布时间】:2020-01-23 05:30:49
【问题描述】:

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

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

我把功能“组织者”放在这里,因为我无法找到问题所在,所以可能需要更多信息。 我一直在沿函数使用 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)

这是结果:(相同的列表 10 次)

[['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']

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

【问题讨论】:

  • 如果你避免改变输入列表,而是复制它并改变副本,它会更容易理解。所以在organizer()的第一行,做list = list.copy()
  • 与问题无关,但避免使用list等python关键字作为变量名。例如,将其称为myList

标签: python list function append extend


【解决方案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

【讨论】:

    【解决方案2】:

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

    在您的 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
    

    【讨论】:

      猜你喜欢
      • 2021-08-18
      • 2019-09-22
      • 1970-01-01
      • 1970-01-01
      • 2022-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-22
      相关资源
      最近更新 更多