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?
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.
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.
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.
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.
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.
The repeat function, as name suggests does the task of repetition and grouping into a list is done by the from_iterable function.
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)
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.
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])
Using the repeat function of NumPy you can get the solution
import numpy as np
np.repeat(A, A)
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]
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]
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With