根据列表列表中的计数重复列表列表中的值

时间:2018-05-30 12:31:10

标签: python

我有一份列表清单,如下所示:

rep = [[[1, 3, 5, 3, 7, 43], 
        [6, 5, 2, 56, 4, 12], 
        [5, 2, 5, 2, 5, 7, 1]], 
       [[5, 3, 7, 3, 21], 
        [6, 2, 5, 2,1, 6, 2], 
        [78, 2, 65, 2, 6, 2], 
        [78, 2, 65, 2, 6, 2]]]

表示所需重复次数的列表清单

count = [[5, 2, 5], [5, 2, 4, 5]]

如您所见,count中每个元素的长度与rep中每个列表的长度相同。

我想使用count重复rep中最内层的列表。

例如,输出应如下所示:

out = [[[1, 3, 5, 3, 7, 43], 
        [1, 3, 5, 3, 7, 43], 
        [1, 3, 5, 3, 7, 43], 
        [1, 3, 5, 3, 7, 43], 
        [1, 3, 5, 3, 7, 43], 
        [6, 5, 2, 56, 4, 12], 
        [6, 5, 2, 56, 4, 12], 
        [5, 2, 5, 2, 5, 7, 1], 
        [5, 2, 5, 2, 5, 7, 1], 
        [5, 2, 5, 2, 5, 7, 1], 
        [5, 2, 5, 2, 5, 7, 1] 
        [5, 2, 5, 2, 5, 7, 1]], 
       [[5, 3, 7, 3, 21], 
        [5, 3, 7, 3, 21], 
        [5, 3, 7, 3, 21], 
        [5, 3, 7, 3, 21], 
        [5, 3, 7, 3, 21], 
        [6, 2, 5, 2,1, 6, 2], 
        [6, 2, 5, 2,1, 6, 2], 
        [78, 2, 65, 2, 6, 2], 
        [78, 2, 65, 2, 6, 2], 
        [78, 2, 65, 2, 6, 2], 
        [78, 2, 65, 2, 6, 2], 
        [78, 2, 98, 2, 6, 2], 
        [78, 2, 98, 2, 6, 2], 
        [78, 2, 98, 2, 6, 2], 
        [78, 2, 98, 2, 6, 2], 
        [78, 2, 98, 2, 6, 2]]]

我该怎么做?

3 个答案:

答案 0 :(得分:2)

您可以对zip使用嵌套列表理解:

res = [[x for x, y in zip(sub_a, sub_b) for _ in range(y)] for sub_a, sub_b in zip(rep, count)]

对于您的示例,返回:

[[[1, 3, 5, 3, 7, 43], 
  [1, 3, 5, 3, 7, 43], 
  [1, 3, 5, 3, 7, 43], 
  [1, 3, 5, 3, 7, 43], 
  [1, 3, 5, 3, 7, 43], 
  [6, 5, 2, 56, 4, 12], 
  [6, 5, 2, 56, 4, 12], 
  [5, 2, 5, 2, 5, 7, 1], 
  [5, 2, 5, 2, 5, 7, 1], 
  [5, 2, 5, 2, 5, 7, 1], 
  [5, 2, 5, 2, 5, 7, 1], 
  [5, 2, 5, 2, 5, 7, 1]], 
 [[5, 3, 7, 3, 21], ...]
  

仅供参考:您的问题以编程方式有趣;这不是降票的来源。没有表现出任何努力就是问题。

答案 1 :(得分:1)

您可以将列表理解与ziprepeatreduce结合使用,以获得结果。

from functools import reduce
from operator import add
from itertools import repeat

[reduce(add, [list(repeat(r, c)) for r,c in zip(rp, cnt)]) for rp, cnt in zip(rep, count)]

# returns:
[[[1, 3, 5, 3, 7, 43],
  [1, 3, 5, 3, 7, 43],
  [1, 3, 5, 3, 7, 43],
  [1, 3, 5, 3, 7, 43],
  [1, 3, 5, 3, 7, 43],
  [6, 5, 2, 56, 4, 12],
  [6, 5, 2, 56, 4, 12],
  [5, 2, 5, 2, 5, 7, 1],
  [5, 2, 5, 2, 5, 7, 1],
  [5, 2, 5, 2, 5, 7, 1],
  [5, 2, 5, 2, 5, 7, 1],
  [5, 2, 5, 2, 5, 7, 1]],
 [[5, 3, 7, 3, 21],
  [5, 3, 7, 3, 21],
  [5, 3, 7, 3, 21],
  [5, 3, 7, 3, 21],
  [5, 3, 7, 3, 21],
  [6, 2, 5, 2, 1, 6, 2],
  [6, 2, 5, 2, 1, 6, 2],
  [78, 2, 65, 2, 6, 2],
  [78, 2, 65, 2, 6, 2],
  [78, 2, 65, 2, 6, 2],
  [78, 2, 65, 2, 6, 2],
  [78, 2, 65, 2, 6, 2],
  [78, 2, 65, 2, 6, 2],
  [78, 2, 65, 2, 6, 2],
  [78, 2, 65, 2, 6, 2],
  [78, 2, 65, 2, 6, 2]]]

答案 2 :(得分:1)

简化问题的前提,您正在寻找重复列表的方法。

>>> list = [1,2,3]
>>> count = 3
>>> [item for item in list for _ in range(count)]
[1, 1, 1, 2, 2, 2, 3, 3, 3]

升级一次,你有一个需要重复的列表列表,以及重复计数列表。

>>> lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> counts = [2, 1, 3]
>>> [l for l, c in zip(lists, counts) for _ in range(c)]
[[1, 2, 3], [1, 2, 3], [4, 5, 6], [7, 8, 9], [7, 8, 9], [7, 8, 9]]

最后一次迭代,再次添加一级嵌套。

>>> lists = [[[1, 2], [2, 4]], [[5, 6], [7, 8]]]
>>> counts = [[1, 2], [3, 4]]
>>> [[l for l, c in zip(sublist, subcounts) for _ in range(c)] for sublist, subcounts in zip(lists, counts)]
[[[1, 2], [2, 4], [2, 4]], [[5, 6], [5, 6], [5, 6], [7, 8], [7, 8], [7, 8], [7, 8]]]