【问题标题】:Turning a list into nested lists in python在python中将列表转换为嵌套列表
【发布时间】:2011-09-30 16:32:32
【问题描述】:

可能重复:
How can I turn a list into an array in python?

我怎样才能打开一个列表,例如:

data_list = [0,1,2,3,4,5,6,7,8]

进入列表列表,例如:

new_list = [ [0,1,2] , [3,4,5] , [6,7,8] ]

即我想将列表中的有序元素分组并将它们保存在有序列表中。我该怎么做?

谢谢

【问题讨论】:

  • 你在不到一小时前就问过了。
  • 抱歉 - 我无法确定在您的问题上定义子组的标准。在您的示例中,所有数字都是“有序的” - 那么您是否想要有序元素和最大长度为 3 的子组?或者每个子组只有 3 长?
  • @Sentinel:不严格重复,因为不涉及numpy。但也许足够接近。谢谢
  • 这根本不是引用问题的重复。 listnumpy.array 的解决方案完全不同。
  • @Sentinel:你说得对,它很相似,但我现在需要使用列表而不是数组。当我使用上一个问题中的 np.reshape 方法恢复到一维数组时,我丢失了所有内部分组——我的数据不仅仅是 [1,2,3...],而是很多 3 元组。我需要一种不使用数组将组织重新组合成三部分的方法。我认为这个问题本身就很有用,并且与前一个问题有很大不同。

标签: python list nested


【解决方案1】:

您的原始列表中是否有任何类型的选择标准?

Python 确实允许您这样做:

new_list = []
new_list.append(data_list[:3])
new_list.append(data_list[3:6])
new_list.append(data_list[6:])

print new_list
# Output:  [ [0,1,2] , [3,4,5] , [6,7,8] ]

【讨论】:

  • FWIW,当您尝试概括描述的模式 OP 时,这不是很实用。谢谢
  • 哦,我不知道 OP 是否想知道 Python 中是否可以嵌入列表,或者是否有办法将列表转换为一个列表。
【解决方案2】:

类似:

map (lambda x: data_list[3*x:(x+1)*3], range (3))

【讨论】:

    【解决方案3】:

    这假设 data_list 的长度是三的倍数

    i=0
    new_list=[]
    while i<len(data_list):
      new_list.append(data_list[i:i+3])
      i+=3
    

    【讨论】:

      【解决方案4】:

      new_list = [data_list[x:x+3] for x in range(0, len(data_list) - 2, 3)]

      列出获胜的理解:)

      【讨论】:

        【解决方案5】:

        这将按照它们出现的顺序对每 3 个元素进行分组:

        new_list = [data_list[i:i+3] for i in range(0, len(data_list), 3)]
        

        如果不是你想要的,给我们一个更好的例子。

        【讨论】:

        • 我遇到了与上述问题类似的情况,您的代码运行完美!我一直在最后阶段挣扎。很高兴我能解决这个问题。
        【解决方案6】:

        基于the answer from Fred Foo,如果你已经在使用numpy,你可以使用reshape得到一个二维数组而不复制数据:

        import numpy
        new_list = numpy.array(data_list).reshape(-1, 3)
        

        【讨论】:

          【解决方案7】:

          以下函数扩展原始上下文以包含任何所需的列表结构列表:

          def gen_list_of_lists(original_list, new_structure):
              assert len(original_list) == sum(new_structure), \
              "The number of elements in the original list and desired structure don't match"
                  
              list_of_lists = [[original_list[i + sum(new_structure[:j])] for i in range(new_structure[j])] \
                               for j in range(len(new_structure))]
                  
              return list_of_lists
          

          使用上面的:

          data_list = [0,1,2,3,4,5,6,7,8]
              
          new_list = gen_list_of_lists(original_list=data_list, new_structure=[3,3,3])
          # The original desired outcome of [[0,1,2], [3,4,5], [6,7,8]]
          
          new_list = gen_list_of_lists(original_list=data_list, new_structure=[2,3,3,1])
          # [[0, 1], [2, 3, 4], [5, 6, 7], [8]]
          

          【讨论】:

            【解决方案8】:

            下面的更优化,也很简单。

            data_list = [0,1,2,3,4,5,6,7,8]
            result =[]
            i=0
            while i <(len(data_list)-2):
                result.append(data_list[i:i+3])
                i+=3
            print(result)
            
            **output**
            

            [[0, 1, 2], [3, 4, 5], [6, 7, 8]]

            【讨论】:

            • 我修复了代码。很多错误。但是,我不确定这是否能解决问题。否决答案。
            • @JoeFerndz 我能知道你在这方面做了什么吗?你运行代码了吗?
            • 因为我记得写了工作代码。你做了哪些改变?
            猜你喜欢
            • 1970-01-01
            • 2014-08-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-04-03
            • 2018-11-12
            • 1970-01-01
            相关资源
            最近更新 更多