【问题标题】:How to Remove nested list which is inside a nested list in python?如何删除python嵌套列表中的嵌套列表?
【发布时间】:2021-06-23 20:45:22
【问题描述】:

我想删除嵌套列表中的所有嵌套列表。这意味着

x = [['-e'], ['-d', ['-e'], '-d'], ['-c', ['-d', ['-e'], '-d'], '-c'], ['-b', ['-c', ['-d', ['-e'], '-d'], '-c'], '-b'], ['-a', ['-b', ['-c', ['-d', ['-e'], '-d'], '-c'], '-b'], '-a']]

我想删除索引 1,2,3,4... 中的嵌套列表,并使其成为平面列表。为了清楚起见,下面是列表中的分隔值。

['-e']
['-d', ['-e'], '-d']
['-c', ['-d', ['-e'], '-d'], '-c']
['-b', ['-c', ['-d', ['-e'], '-d'], '-c'], '-b']
['-a', ['-b', ['-c', ['-d', ['-e'], '-d'], '-c'], '-b'], '-a']

我想要它

[['-e'], ['-d', '-e', '-d'], ['-c', '-d', '-e', '-d', '-c'], ['-b', '-c', '-d', '-e', '-d', '-c', '-b'], ['-a', '-b', '-c', '-d', '-e', '-d', '-c', '-b', '-a']]

['-e']
['-d', '-e', '-d']
['-c', '-d', '-e', '-d', '-c']
['-b', '-c', '-d', '-e', '-d', '-c', '-b']
['-a', '-b', '-c', '-d', '-e', '-d', '-c', '-b', '-a']

有没有办法像上面那样获得输入。

for i in range(0,size):
        z = ['-{}'.format(alnum[size-i])]
        if alnum[size] != alnum[size-i]:
            x.append(z*2)
        else:
            x.append(z)

这是我用来获取 x 列表的 sn-p。

【问题讨论】:

    标签: python list nested-lists


    【解决方案1】:

    具有用于展平嵌套子列表的辅助递归函数以及列表理解:

    def nested_flatten(seq):
        # start with an empty list
        result = []
        
        # for each item in the sublist...
        for item in seq:
            # is item a list?
            if isinstance(item, list):
                # then extend the result with the flattened item
                result += nested_flatten(item)
            else:
                # otherwise extend with the item itself
                result += [item]
        return result
    
    # invoke `nested_flatten` on each sublist
    out = [nested_flatten(sub) for sub in x]
    

    得到

    >>> out
    
    [['-e'],
     ['-d', '-e', '-d'],
     ['-c', '-d', '-e', '-d', '-c'],
     ['-b', '-c', '-d', '-e', '-d', '-c', '-b'],
     ['-a', '-b', '-c', '-d', '-e', '-d', '-c', '-b', '-a']]
    

    【讨论】:

    • 我是初学者,你能解释一下吗。'result += nested_flatten(item)'
    • @GoAmeer030 该函数以一个空列表开始。然后检查子列表中的每个项目,例如,['-c', ['-d', ['-e'], '-d'], '-c']。如果该项目不是列表(例如第一个),则将其按原样放入result 列表中。但如果它是一个列表(例如第二个['-d', ['-e'], '-d']),那么它会调用自己!这个过程还在继续。最后的每个调用都会返回一个列表。最后的列表推导为x 的每个元素调用此函数。
    • @GoAmeer030 如果你对递归函数不是很熟悉,那我可能会让你更加困惑……抱歉,如果是这样的话,但我希望它有所帮助……
    【解决方案2】:

    python-convert-list-of-lists-or-nested-list-to-flat-list

    def flattenNestedList(nestedList):
        ''' Converts a nested list to a flat list '''
        flatList = []
        # Iterate over all the elements in given list
        for elem in nestedList:
            # Check if type of element is list
            if isinstance(elem, list):
                # Extend the flat list by adding contents of this element (list)
                flatList.extend(flattenNestedList(elem))
            else:
                # Append the elemengt to the list
                flatList.append(elem)    
        return flatList
    
    x = [['-e'], ['-d', ['-e'], '-d'], ['-c', ['-d', ['-e'], '-d'], '-c'], ['-b', ['-c', ['-d', ['-e'], '-d'], '-c'], '-b'], ['-a', ['-b', ['-c', ['-d', ['-e'], '-d'], '-c'], '-b'], '-a']]
    
    for li in x:
        print(flattenNestedList(li))
    

    输出:

    ['-e']
    ['-d', '-e', '-d']
    ['-c', '-d', '-e', '-d', '-c']
    ['-b', '-c', '-d', '-e', '-d', '-c', '-b']
    ['-a', '-b', '-c', '-d', '-e', '-d', '-c', '-b', '-a']
    

    【讨论】:

      【解决方案3】:

      这段代码使用递归来实现你所需要的。

      from copy import deepcopy
      
      x = [
          ['-e'],
          ['-d', ['-e'], '-d'],
          ['-c', ['-d', ['-e'], '-d'], '-c'],
          ['-b', ['-c', ['-d', ['-e'], '-d'], '-c'], '-b'],
          ['-a', ['-b', ['-c', ['-d', ['-e'], '-d'], '-c'], '-b'], '-a']
      ]
      
      temp_list = []
      def delist(x):
          for i in x:
              if type(i) == list:
                  delist(i)
              else:
                  temp_list.append(i)
      
      new_list = []
      for item in x:
          delist(item)
          new_list.append(deepcopy(temp_list))
          temp_list.clear()
      
      print('Resultant:', new_list)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-02-01
        • 1970-01-01
        • 2022-12-12
        • 1970-01-01
        • 1970-01-01
        • 2019-07-13
        • 1970-01-01
        • 2021-08-21
        相关资源
        最近更新 更多