【问题标题】:How to continually split a list and separate remainder?如何不断拆分列表并分离剩余部分?
【发布时间】:2018-01-14 13:07:54
【问题描述】:

我将如何将一个列表一分为二,同时留下一个剩余部分作为它自己的“列表”?例如:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14], [15]

不仅如此,还要继续将每个子列表减半以获得所需的结果:

[1, 2, 3], [4, 5, 6], [7], [8, 9, 10], [11, 12, 13], [14], [15]

【问题讨论】:

  • 您自己尝试过吗?什么时候应该停止分裂?
  • 我试过把它拆分一次,但不知道从那里去哪里,只能分成相等的部分。当模式与底部列表匹配时,拆分应该停止,其中 2 个 3 的列表后跟 1 个 1 的“列表”(因为我使用的所有列表都会有一个余数)。
  • 您可以将edit 加入您的问题中。
  • 你的结束条件不是很清楚;您的第二步匹配相同的条件(两个长度相等的列表和一个余数)。我将其解释为具有无法进一步拆分而不会成为单值列表的列表。所以长度为 3 或更低的任何内容。

标签: python list split


【解决方案1】:

下面的代码应该可以解决问题:

def splitList(array):
    half = len(array) // 2 #find the halfway point
    return (array[:half], #get the first half
           array[half : half*2], #get the second half
           array[half*2 : half*2 + len(array)%2] #get the remainder
           )

array = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
splitList(array)

>> ['a', 'b', 'c'], ['d', 'e', 'f'], ['g']

然后重复任意多次:

array = [['a', 'b', 'c', 'd', 'e', 'f', 'g']]
for x in range(n):
    newArray = []
    for sub in array:
        newArray += splitList(sub)
    array = newArray
    array = [e for e in array if e != []] #remove empty elements

【讨论】:

    【解决方案2】:

    您可以使用递归生成器:

    def split_gen(lst, start=0, stop=None):
        if stop is None:
            stop = len(lst)
        size = (stop - start)
        if size < 4:
            yield lst[start:stop]
            return
        half = size // 2
        # two chunks of length half, plus an optional remainder
        yield from split_gen(lst, start, start + half)
        yield from split_gen(lst, start + half, start + (half * 2))
        if size % 2 == 1:
            yield lst[stop - 1:stop]
    

    演示:

    >>> list(split_gen([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]))
    [[1, 2, 3], [4, 5, 6], [7], [8, 9, 10], [11, 12, 13], [14], [15]]
    

    请注意,它不会生成中间列表;它只计算拆分的索引,只创建最终的子列表。

    【讨论】:

      【解决方案3】:

      您可以使用两个函数,一个将初始列表拆分为部分列表,另一个对生成的列表进行递归调用,直到无法进一步拆分 (len(sub-list) &lt;= 3),例如:

      l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
      
      def split_list(lst):
          if len(lst) > 3:
              n = len(lst) // 2  # integer division to get the size of the new sublists
              new_list = list([lst[0:n]])  # append sublist 1
              new_list.append(lst[n:n+n])  # append sublist 2
              new_list.append(lst[len(lst)-len(lst) % 2:])  # get remainder and append to list
          return split_sub(new_list)  
      
      def split_sub(lst):
          chk = 0
          new_list = []
          for item in lst:
              if len(item) > 3:
                  n = len(item) // 2
                  new_list.append(list(item[0:n]))
                  new_list.append(item[n:n + n])
                  new_list.append(item[len(item) - len(item) % 2:])
              else:
                  new_list.append(item)
                  chk += 1
          if len(new_list) == chk:  # if this condition is met then all sub lists are of the size <= 3
              return new_list
          return split_sub(new_list)
      
      print split_list(l)
      

      第二个函数将一直运行到所有len(sub-list) &lt;= 3,这意味着它完成了拆分并将返回最终列表。

      输出:

      [[1, 2, 3], [4, 5, 6], [7], [8, 9, 10], [11, 12, 13], [14], [15]]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-31
        • 2020-12-22
        • 2011-04-03
        • 1970-01-01
        相关资源
        最近更新 更多