How to create duplicate for each value in a python list given the number of dups I want?

2024/11/19 14:50:24

I have this list:

a=[7086, 4914, 1321, 1887, 7060]. Now, I want to create duplicate of each value n-times. Such as:

n=2a=[7086,7086,4914,4914,1321,1321,7060,7060]

How would I do this best? I tried a loop but it was inefficient.

Answer

The idiomatic way in Python is to use zip and then flatten the resulting list of tuples.

As stated in the docs:

The left-to-right evaluation order of the iterables is guaranteed. This makes possible an idiom for clustering a data series into n-length groups using zip(*[iter(s)]*n).

So in this case:

>>> a=[7086, 4914, 1321, 1887, 7060]
>>> n=2
>>> [e for t in zip(*[a]*n) for e in t]
[7086, 7086, 4914, 4914, 1321, 1321, 1887, 1887, 7060, 7060]

Alternatively, just flatten a constructed list of sublists made [list_item]*n like so:

>>> [e for sl in ([e]*n for e in a) for e in sl]
# same output

Which can then be constructed as a generator which is as efficient as possible here:

>>> it=(e for sl in ([e]*n for e in a) for e in sl)
>>> next(it)
7086
>>> next(it)
7086
>>> next(it)
4914
>>> ...
https://en.xdnf.cn/q/119940.html

Related Q&A

How do I generate random float and round it to 1 decimal place

How would I go about generating a random float and then rounding that float to the nearest decimal point in Python 3.4?

Error extracting text from website: AttributeError NoneType object has no attribute get_text

I am scraping this website and get "title" and "category" as text using .get_text().strip().I have a problem using the same approach for extracting the "author" as text.d…

Fastest way to extract tar files using Python

I have to extract hundreds of tar.bz files each with size of 5GB. So tried the following code:import tarfile from multiprocessing import Poolfiles = glob.glob(D:\\*.tar.bz) ##All my files are in D for …

Python - Split a string but keep contiguous uppercase letters [duplicate]

This question already has answers here:Splitting on group of capital letters in python(3 answers)Closed 3 years ago.I would like to split strings to separate words by capital letters, but if it contain…

Python: Find a Sentence between some website-tags using regex

I want to find a sentence between the ...class="question-hyperlink"> tags. With this code:import urllib2 import reresponse = urllib2.urlopen(https://stackoverflow.com/questions/tagged/pyth…

How to download all the href (pdf) inside a class with python beautiful soup?

I have around 900 pages and each page contains 10 buttons (each button has pdf). I want to download all the pdfs - the program should browse to all the pages and download the pdfs one by one. Code only…

Reducing the complexity/computation time for a basic graph formula [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 4 years ago.Improve…

Find All Possible Fixed Size String Python

Problem: I want to generate all possible combination from 36 characters that consist of alphabet and numbers in a fixed length string. Assume that the term "fixed length" is the upper bound f…

What is the concept of namespace when importing a function from another module?

main.py:from module1 import some_function x=10 some_function()module1.py:def some_function():print str(x)When I execute the main.py, it gives an error in the moduel1.py indicating that x is not availab…

How to pass a literal value to a kedro node? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 4 years ago.This po…