Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to repeat the numbers in a list in python?

Tags:

python

list

I have a list

 A = [1,6,3,8,5,5,2,1,2,10]

I want to repeat the numbers in this like:

A = [1,6,6,6,6,6,6,3,3,3,8,8,8,8,8,8,8,8,..... so on] 

i.e 1 repeat once, 6 repeat six times, 3 repeat thrice and so on....

I tried with:

B=np.concatenate([([x]*x) for x in A], axis=0) 

but it multiplying the corresponding number and I am getting this result:

  B = [1,36,36,36,36,36,36,9,9,9,.....so on]

when I am doing:

B=np.concatenate([([x]*3) for x in A], axis=0)

this giving me:

B = [1,1,6,6,3,3,8,8... so on]

what wrong I am doing here?

like image 924
ED314 Avatar asked Jun 16 '21 12:06

ED314


People also ask

How do you repeat data in Python?

In Python, if you want to repeat the elements multiple times in the NumPy array then you can use the numpy. repeat() function. In Python, this method is available in the NumPy module and this function is used to return the numpy array of the repeated items along with axis such as 0 and 1.

How do you repeat a list in a loop Python?

To repeat each element k times in a list, We will first create an empty list named newList . After that, we will traverse each element of the input list. During traversal, we will add each element of the existing list to the newly created list k times using a for loop, range() method, and the append() method.

Is repetition allowed in list in Python?

Output. Note that Python creates shallow copies of the lists in this. So changing objects at one place will change them at all places they are repeated. If you don't want this behaviour, don't use repetition operator to create lists.

What is repetition of list in Python?

Python list repetition operator (*) is used to repeat a list, number of times which is given by the integer value and create a new list values.

How to repeat elements of a list in Python?

In this article, we will look at different ways to repeat elements of a list in python. How to repeat elements of a list in Python To repeat the elements in a list in python, we insert the existing elements of a list to the same list. In this way, the elements in a list get repeated.

How does the repeat function work in Python?

The repeat function, as name suggests does the task of repetition and grouping into a list is done by the from_iterable function.

How do you repeat an array in NumPy?

np.repeat (a, repeats) will repeat the input array a according to repeats which specify the number of repetitions for each element. import numpy as np A = [1,6,3,8,5,5,2,1,2,10] B = np.repeat (A,A)

How to repeat elements of a list using extend() method?

The given list is: [1, 2, 3, 4, 5] The input list to append elements from is: [6, 7] The output list is: [1, 2, 3, 4, 5, 6, 7] To repeat elements of a list using the extend() method, we will copy the elements of the existing list in a temporary list.


5 Answers

You can perform this operation using numpy without using a for loop.

np.repeat(a, repeats) will repeat the input array a according to repeats which specify the number of repetitions for each element.

import numpy as np
A = [1,6,3,8,5,5,2,1,2,10]
B = np.repeat(A,A)

Output:

>>> array([ 1,  6,  6,  6,  6,  6,  6,  3,  3,  3,  8,  8,  8,  8,  8,  8,  8,
    8,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  2,  2,  1,  2,  2, 10,
   10, 10, 10, 10, 10, 10, 10, 10, 10])
like image 145
Antoine Dubuis Avatar answered Oct 21 '22 04:10

Antoine Dubuis


Using the repeat function of NumPy you can get the solution

import numpy as np
np.repeat(A, A)
like image 36
nhm Avatar answered Oct 21 '22 05:10

nhm


You should use nested loop

l  = [1, 6, 3]
nl = []
for number in l:
    for i in range(number):
        nl.append(number)
print(nl)

or using list comprehension

l = [1,6,3]
nl = [number  for number in l for i in range(number)]
#[1, 6, 6, 6, 6, 6, 6, 3, 3, 3] 
like image 45
Techtanic Avatar answered Oct 21 '22 05:10

Techtanic


hello you can make this with the lib itertools:

import itertools
lst = [1,2,3,4,5]
# [1, 2, 3, 4, 5]
list(itertools.chain.from_iterable(itertools.repeat(x, x) for x in lst)) 
#[1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]
like image 3
Tomo Avatar answered Oct 21 '22 04:10

Tomo


You have a list of integers, to which the multiplication means arithmetic multiplication. You need to convert them into strings.

A = [1,6,3,8,5,5,2,1,2,10]
new_A = [x * str(x) for x in A]

which is called list comprehension, being a much cleaner/pythonic way of:

for x in A:
new_A.append(x * str(x))
like image 3
Raspberry PyCharm Avatar answered Oct 21 '22 03:10

Raspberry PyCharm