【问题标题】:How to repeat list's elements with each different n times and in Python?python - 如何在每个不同的n次和Python中重复列表的元素?
【发布时间】:2021-01-31 06:21:37
【问题描述】:

这个想法是重复列表的元素每个不同的n 次,如下所示。

ls = [7, 3, 11, 5, 2, 3, 4, 4, 2, 3]
id_list_fname = ['S11', 'S15', 'S16', 'S17', 'S19', 'S3', 'S4', 'S5', 'S6', 'S9']
all_ls = []
for id, repeat in zip(id_list_fname, ls):
    res = [ele for ele in[id] for i in range(repeat)]
    all_ls.append(res)

因此,我希望结果是一个单一的平面列表,如下所示。

def flatten(lst):
    for item in lst:
        if isinstance(item, list):
            yield from flatten(item)
        else:
            yield item

final_output = list(flatten(all_ls))

final_output 的输出:

['S11', 'S11', 'S11', 'S11', 'S11', 'S11', 'S11', 'S15', 'S15', 'S15',
 'S16', 'S16', 'S16', 'S16', 'S16', 'S16', 'S16', 'S16', 'S16', 'S16',
 'S16', 'S17', 'S17', 'S17', 'S17', 'S17', 'S19', 'S19', 'S3', 'S3',
 'S3', 'S4', 'S4', 'S4', 'S4', 'S5', 'S5', 'S5', 'S5', 'S6', 'S6',
 'S9', 'S9', 'S9']

但我想知道是否存在更紧凑的方法或技术,例如 itertools 以使重复元素与我使用上面的代码 sn-p 实现的一样。

【问题讨论】:

  • 你能给出一个最小的示例输入和期望的输出吗?
  • 您好@lain,感谢您对此 OP 的关注。上面已经提供了示例输入和所需输出。
  • @balandongiv 我没有看到预期的输出。
  • 嗨@yudhiesh,请查看修改后的版本

标签: python list iterator


【解决方案1】:

您可以使用itertools.chainitertools.repeat

from itertools import chain, repeat

ls = [7, 3, 11, 5, 2, 3, 4, 4, 2, 3]
id_list_fname = ['S11', 'S15', 'S16', 'S17', 'S19', 'S3', 'S4', 'S5', 'S6', 'S9']
res = list(chain.from_iterable(repeat(j, times = i) for i, j in zip(ls, id_list_fname)))

print(res)

输出

['S11', 'S11', 'S11', 'S11', 'S11', 'S11', 'S11', 'S15', 'S15', 'S15', 'S16', 'S16', 'S16', 'S16', 'S16', 'S16', 'S16', 'S16', 'S16', 'S16', 'S16', 'S17', 'S17', 'S17', 'S17', 'S17', 'S19', 'S19', 'S3', 'S3', 'S3', 'S4', 'S4', 'S4', 'S4', 'S5', 'S5', 'S5', 'S5', 'S6', 'S6', 'S9', 'S9', 'S9']

【讨论】:

    【解决方案2】:

    关于您的代码,在向all_ls 添加项目时,请使用list.extend() 而不是list.append()。这会将列表中的项目添加到现有列表中,而不是将列表添加到现有列表中。

    ls = [7, 3, 11, 5, 2, 3, 4, 4, 2, 3]
    id_list_fname = ['S11', 'S15', 'S16', 'S17', 'S19', 'S3', 'S4', 'S5', 'S6', 'S9']
    all_ls=[]
    for s,repeat in zip(id_list_fname ,ls):
        res =  [ele for ele in [s] for i in range(repeat)]
        all_ls.extend(res)
    

    输出

    >>> all_ls
    ['S11', 'S11', 'S11', 'S11', 'S11', 'S11', 'S11', 'S15', 'S15', 'S15', 'S16', 'S16', 'S16', 'S16', 'S16', 'S16', 'S16', 'S16', 'S16', 'S16', 'S16', 'S17', 'S17', 'S17', 'S17', 'S17', 'S19', 'S19', 'S3', 'S3', 'S3', 'S4', 'S4', 'S4', 'S4', 'S5', 'S5', 'S5', 'S5', 'S6', 'S6', 'S9', 'S9', 'S9']
    

    更好的方法是使用列表理解 - 它可以在一行中完成:

    >>> [item for n,s in zip(ls, id_list_fname) for item in [s]*n]
    ['S11', 'S11', 'S11', 'S11', 'S11', 'S11', 'S11', 'S15', 'S15', 'S15', 'S16', 'S16', 'S16', 'S16', 'S16', 'S16', 'S16', 'S16', 'S16', 'S16', 'S16', 'S17', 'S17', 'S17', 'S17', 'S17', 'S19', 'S19', 'S3', 'S3', 'S3', 'S4', 'S4', 'S4', 'S4', 'S5', 'S5', 'S5', 'S5', 'S6', 'S6', 'S9', 'S9', 'S9']
    

    【讨论】:

    • dict 理解可以替换为res = [id] * repeatid 不应该在 Python 中用作变量名,您正在覆盖内置函数 id
    • @IainShelvington:什么是字典理解? id 很公平,刚刚从 OP 复制。已更新。
    • @IainShelvington:此外,对 OP 代码的最小更改有助于 OP 理解差异。我添加了一个更好的列表理解来在一行中执行相同的任务。
    • 感谢@mhawke,从您的帖子中获得了额外的知识。
    • @mhawke 我的意思是列表理解,道歉
    【解决方案3】:

    Numpy 有repeat 函数:

    np.repeat(id_list_fname, ls)
    

    如果你绝对需要一份清单:

    np.repeat(id_list_fname, ls).tolist()
    

    如果您不想使用 numpy,请记住 python 允许嵌套推导,这本质上是多个 for 循环创建单个平面列表。你的原始循环可以写成一个扁平的单行:

    [id for id, repeat in zip(id_list_fname, ls) for _ in range(repeat)]
    

    【讨论】:

      猜你喜欢
      • 2018-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-12
      • 1970-01-01
      • 2021-02-21
      相关资源
      最近更新 更多