【问题标题】:How can I create a nested list and put a specific amount of elements in each nest? list and It's nested elements always not the same如何创建嵌套列表并在每个嵌套中放置特定数量的元素? list 和它的嵌套元素总是不一样的
【发布时间】:2021-08-04 21:19:54
【问题描述】:

这就是我正在做的,

    amount = [2, 2, 3]
my_list = ['name_zero', 'name_one', 'score_three', 'score_nine', 'age_zero', 'age_ten', 'age_six']
output = []

for i in amount:
    output.append(my_list[:i])

print(output)
  • 我得到了什么:

    [['name_zero', 'name_one'], ['name_zero', 'name_one'], ['name_zero', 'name_one', 'score_three']]

我的代码的问题是,它附加了相同的元素,接收这样的输出的正确方法应该是什么?

# [['name_zero', 'name_one'], ['score_three', 'score_nine'], ['age_zero', 'age_ten', 'age_six']]

【问题讨论】:

    标签: python python-3.x list nested-lists


    【解决方案1】:

    试试:

    amount = [2, 2, 3]
    my_list = [
        "name_zero",
        "name_one",
        "score_three",
        "score_nine",
        "age_zero",
        "age_ten",
        "age_six",
    ]
    output = []
    
    i = iter(my_list)
    for v in amount:
        output.append([next(i) for _ in range(v)])
    
    print(output)
    

    打印:

    [
        ["name_zero", "name_one"],
        ["score_three", "score_nine"],
        ["age_zero", "age_ten", "age_six"],
    ]
    

    【讨论】:

      【解决方案2】:

      一种方法是跟踪起始索引,然后跟踪每个列表的长度

      amount = [2, 2, 3]
      my_list = ['name_zero', 'name_one', 'score_three', 'score_nine', 'age_zero', 'age_ten', 'age_six']
      output = []
      
      start_index = 0
      for list_len in amount:
          output.append(my_list[start_index:(start_index + list_len)])
          start_index += list_len
      
      print(output)
      

      【讨论】:

        【解决方案3】:

        实际上,您必须指定 output.append(my_list[:i]) 的起始索引,因为它在每次迭代时都会发生变化。应该这样做。

        amount = [2, 2, 3]
        my_list = ['name_zero', 'name_one', 'score_three', 'score_nine', 'age_zero', 'age_ten', 'age_six']
        output = []
        j = 0
        for i in amount:
            output.append(my_list[j:j+i])
            j += i
        
        print(output)
        

        输出:

        [['name_zero', 'name_one'], ['score_three', 'score_nine'], ['age_zero', 'age_ten', 'age_six']]
        

        编辑:有一种更 Pythonic 的方式来做同样的事情:

        output = [my_list[sum(amount[:i]):sum(amount[:i+1])] for i in range(len(amount))]
        

        你也可以考虑这个:

        output = iter(my_list)
        output = [[next(output) for i in range(i)] for i in amount]
        

        【讨论】:

          【解决方案4】:

          这应该可以解决问题:

          amount = [2,2,3]
          my_list = ['name_zero', 'name_one', 'score_three', 'score_nine', 'age_zero', 'age_ten', 'age_six']
          output = []
          
          for i in amount:
              count = 0
              current_list = []
              while count < i:
                  current_list.append(my_list.pop(0))
                  count += 1
              output.append(current_list)
          
          print(output)
          
          ['name_zero', 'name_one'], ['score_three', 'score_nine'], ['age_zero', 'age_ten', 'age_six']]
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-05-30
            • 1970-01-01
            • 1970-01-01
            • 2023-03-22
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多