【问题标题】:How to interleave two lists by chunks如何按块交错两个列表
【发布时间】:2020-01-09 14:04:54
【问题描述】:

我想以特定方式合并两个列表。我希望每 4 个值更改我从中获取值的列表。

这是一个示例,但我正在处理的数据集要大得多:

List1 = [1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8]
List2 = [2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8]

#Expected merged list
[1.1, 1.2, 1.3, 1.4, 2.1, 2.2, 2.3, 2.4, 1.5, 1.6, 1.7, 1.8, 2.5, 2.6, 2.7, 2.8]

这里我从List1 取出前4 个,然后从List2 取出接下来的4 个,然后返回List1 等等。

这类似于Intertwining two lists 问题,但没有:

c = [a[0], b[0], a[1], b[1], ..., a[n], b[n]]

我想要

c = [a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3], ..., a[n-3], a[n-2], a[n-1], a[n], b[n-3], b[n-2], b[n-1], b[n]]

【问题讨论】:

  • 我一头雾水,但这和我想要的不一样
  • 您能详细说明一下吗?我的编辑是否改变了问题的含义?

标签: python python-3.x list


【解决方案1】:

你可以试试这个。你可以使用extend

>>> l=[]
>>> for i in range(0,len(List1),4):
        l.extend(List1[i:i+4])
        l.extend(List2[i:i+4])
>>> l
[1.1, 1.1, 1.1, 1.1, 2.1, 2.1, 2.1, 2.1, 1.2, 1.2, 1.2, 1.2, 2.2, 2.2, 2.2, 2.2]
>>> 

【讨论】:

    【解决方案2】:

    这个呢:

    A = [1.1, 1.1, 1.1, 1.1, 1.2, 1.2, 1.2, 1.2, 1.3, 1.3]
    B = [2.1, 2.1, 2.1, 2.1, 2.2, 2.2, 2.2, 2.2, 2.3, 2.3]
    
    
    sep = 4
    num = len(A)
    iterations = int(num / sep) + 1
    
    merged = []
    for i in range(iterations):
        start = sep * i
        end = sep * (i + 1)
        merged.extend(A[start:end])
        merged.extend(B[start:end])
    
    print(merged)
    
    >>> '[1.1, 1.1, 1.1, 1.1, 2.1, 2.1, 2.1, 2.1, 1.2, 1.2, 1.2, 1.2, 2.2, 2.2, 2.2, 2.2, 1.3, 1.3, 2.3, 2.3]'
    
    

    【讨论】:

      【解决方案3】:

      如果您希望能够处理任何长度的列表(不仅可以被 4 整除,而且不仅仅是两个列表具有相同大小)和任意数量的列表...

      任何列表长度

      这可以处理任何列表长度,包括两个列表的长度完全不同的地方:

      result = []
      it1, it2 = iter(List1), iter(List2)
      while chunk := list(islice(it1, 4)) + list(islice(it2, 4)):
          result += chunk
      

      轻微替代:

      result = []
      it1, it2 = iter(List1), iter(List2)
      while chunk := list(chain(islice(it1, 4), islice(it2, 4))):
          result += chunk
      

      演示:

      >>> from itertools import islice
      >>> List1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
      >>> List2 = ['a', 'b', 'c', 'd', 'e']
      >>> k = 4
      
      >>> result = []
      >>> it1, it2 = iter(List1), iter(List2)
      >>> while chunk := list(islice(it1, k)) + list(islice(it2, k)):
              result += chunk
      
      >>> result
      [1, 2, 3, 4, 'a', 'b', 'c', 'd', 5, 6, 7, 8, 'e', 9, 10, 11, 12, 13, 14]
      

      任意数量的列表(任意长度)

      result = []
      iters = deque(map(iter, Lists))
      while iters:
          it = iters.popleft()
          if chunk := list(islice(it, 4)):
              result += chunk
              iters.append(it)
      

      演示(为清晰起见格式化输出):

      >>> from itertools import islice
      >>> from collections import deque
      >>> Lists = [
          [1.1, 1.1, 1.1, 1.1, 1.2, 1.2, 1.2, 1.2],
          [2.1, 2.1, 2.1, 2.1, 2.2, 2.2, 2.2, 2.2, 2.3],
          [3.1, 3.1, 3.1, 3.1, 3.2, 3.2, 3.2, 3.2, 3.3, 3.4]
      ]
      >>> k = 4
      
      >>> result = []
      >>> iters = deque(map(iter, Lists))
      >>> while iters:
              it = iters.popleft()
              if chunk := list(islice(it, 4)):
                  result += chunk
                  iters.append(it)
      
      >>> result
      [1.1, 1.1, 1.1, 1.1, 2.1, 2.1, 2.1, 2.1, 3.1, 3.1, 3.1, 3.1,
       1.2, 1.2, 1.2, 1.2, 2.2, 2.2, 2.2, 2.2, 3.2, 3.2, 3.2, 3.2,
       2.3, 3.3, 3.4]
      

      【讨论】:

        【解决方案4】:

        作为我的另一个答案,这可以处理任何列表长度,但这也可以处理任何数量的列表:

        def gen(i, lst):
            for j, x in enumerate(lst):
                yield j // 4, i, x
        
        result = [x[2] for x in merge(*(gen(*e) for e in enumerate(Lists)))]
        

        演示:

        >>> from heapq import merge
        >>> Lists = [
            [1.1, 1.1, 1.1, 1.1, 1.2, 1.2, 1.2, 1.2],
            [2.1, 2.1, 2.1, 2.1, 2.2, 2.2, 2.2, 2.2],
            [3.1, 3.1, 3.1, 3.1, 3.2, 3.2, 3.2, 3.2]
        ]
        >>> def gen(i, lst):
                for j, x in enumerate(lst):
                    yield j // 4, i, x
        
        >>> [x[2] for x in merge(*(gen(*e) for e in enumerate(Lists)))]
        [1.1, 1.1, 1.1, 1.1, 2.1, 2.1, 2.1, 2.1, 3.1, 3.1, 3.1, 3.1,
         1.2, 1.2, 1.2, 1.2, 2.2, 2.2, 2.2, 2.2, 3.2, 3.2, 3.2, 3.2]
        

        【讨论】:

          【解决方案5】:

          你可以试试这个:

          l=[List1[0]]*4+[List2[1]]*4+[List1[-1]]*4+[List2[-1]]*4

          【讨论】:

            【解决方案6】:

            这段代码应该可以正常工作:

                lst1 = [1.1, 1.1, 1.1, 1.1, 1.2, 1.2, 1.2, 1.2]
                lst2 = [2.1, 2.1, 2.1, 2.1, 2.2, 2.2, 2.2, 2.2]
            
                def combine(lst1 = [], lst2 = []):
                    result = []
                    for i in range(0,len(lst1), 4):
                        for j in range(i, i + 4):
                            result.append(lst1[i]) 
                        result.append(lst2.pop(0))
                        result.append(lst2.pop(0))
                    for item in lst2:
                        result.append(item)
                    return result
            
                print(combine(lst1, lst2)) # [1.1, 1.1, 1.1, 1.1, 2.1, 2.1, 1.2, 1.2, 1.2, 1.2, 2.1, 2.1, 2.2, 2.2, 2.2, 2.2]
            

            我们遍历第一个列表四次,然后将所有项目添加到我们的结果中,并将前 2 个头元素删除到 lst2 中,最后将 lst2 中剩下的所有内容也加载到我们的结果中。

            【讨论】:

              猜你喜欢
              • 2020-07-09
              • 1970-01-01
              • 1970-01-01
              • 2011-03-01
              • 2020-04-06
              • 2017-12-08
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多