简体   繁体   English

如何在每个不同的 n 次和 Python 中重复列表的元素?

[英]How to repeat list's elements with each different n times and in Python?

The idea is to repeat list's elements with each different n times as below.这个想法是重复列表的元素,每个不同的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)

Consequently, I would like the result to be a single flat list, which I achieved as below.因此,我希望结果是一个单一的平面列表,如下所示。

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

final_output = list(flatten(all_ls))

Output of the final_output : final_output 的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']

But I wonder there exist much more compact approach or technique such as itertools to have the repeat elements as what I achieved using the code snippet above.但我想知道是否存在更紧凑的方法或技术,例如itertools来获得重复元素,就像我使用上面的代码片段实现的那样。

You can use itertools.chain and itertools.repeat您可以使用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)

Output 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']

With respect to your code, use list.extend() rather than list.append() when adding items into all_ls .关于您的代码,在将项目添加到all_ls时使用list.extend()而不是list.append() This adds the items in the list to the existing list instead of adding the list to the existing list.这会将列表中的项目添加到现有列表中,而不是将列表添加到现有列表中。

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)

Output Output

>>> 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']

A better way is with a list comprehension - it can be done in one line:更好的方法是使用列表理解 - 它可以在一行中完成:

>>> [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']

Numpy has the repeat function: Numpy 有repeat function:

np.repeat(id_list_fname, ls)

If you absolutely need a list:如果你绝对需要一份清单:

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

If you don't want to use numpy, keep in mind that python allows nested comprehensions, which are essentially multiple for loops creating a single flat list.如果您不想使用 numpy,请记住 python 允许嵌套推导,这实际上是多个for循环创建单个平面列表。 Your original loop can be written as a flat one-liner:你的原始循环可以写成一个扁平的单行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用itertools重复每个Python列表的元素n次? - How to repeat each of a Python list's elements n times with itertools only? 如何将每个数据帧行重复 n 次(每行的 n 不同)? - How to repeat each dataframe row n number of times (n is different for each row)? 像 Haskell 的重复一样,在 Python 中重复函数组合 n 次 - Repeat a function composition n times in Python like Haskell's repeat 如何重复二维数组的元素 n 次而不将其转换为列表? - how to repeat the elements of a 2d array n times without converting it to a list? Python 列表限制特定元素到N次 - Python list limit specific elements to N times 如何在 pandas 中将每一行重复 n 次,使其看起来像这样? - how to repeat each row n times in pandas so that it looks like this? 如何将列表扩展到一定大小而不重复每个单独的列表元素 n 次? - How to expand a list to a certain size without repeating each individual list elements that n-times? Pandas - 每次添加列时如何重复数据帧n次 - Pandas - How to repeat dataframe n times each time adding a column 如何打印列表中的元素n次 - How to print elements in list n times 按照 python 列表中的自身指示重复每个数据的次数 - Repeat each data for a number of times as indicated by itself in a list for python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM