Array problems in for loop

Question:

I want to go from one array A of 10 elements to the array B of 100 elements.

Each element of B from 0 to 9 is equal to the element 0 of A

Each element of B from 10 to 19 is equal to the element 1 of A

….

Each element of B from 90 to 99 is equal to the element 9 of A

I did the following code but it does not work

a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
A = np.asarray(a)

b = []
for i in range(len(A)*10):
    b.append(0)
    
B = np.asarray(b)   

for i in range(len(A)):
    for j in range(9):
        B[j]=A[i]

Expected result:

B [ 0,0,0,0,0,0,0,0,0,0,
  1,1,1,1,1,1,1,1,1,1,
  2,2,2,2,2,2,2,2,2,2
  ...,
  9,9,9,9,9,9,9,9,9,9 ]
Asked By: chrisis

||

Answers:

You can use numpy and specify how many iterations of each element you want:

import numpy as np
A = [1,2,3,4]
B = [np.full(10, a) for a in A]
print(B)

Or if you prefer to not use numpy, instead use:

A = [1,2,3,4]
B = [[a]*10 for a in A]
print(B)

Giving you the wanted list B

Answered By: JakobVinkas

This works for me:

>>> a = [1,2,3]
>>> [ x for i in a for x in [i]*3]
[1, 1, 1, 2, 2, 2, 3, 3, 3]
>>> 

You may replace 3 with 10 or whatever you like.


Answering the question from Jacob:

>>> [[a]*10 for a in A]
[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3, 3, 3, 3, 3]]
Answered By: lenik

You are saving values only in first 9 list elements. You have to ‘scale’ it by adding i*10 to index.

import numpy as np

a=[0, 1, 2, 3, 4, 5, 6, 7]
A = np.asarray(a)

b = []
for i in range(len(A)**2):
    b.append(0)
    
B = np.asarray(b)   

for i in range(len(A)):
    for j in range(len(A)):
        B[j + i*len(A)]=A[i]

print(B)
Answered By: xszym
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = []
for x in a:
    b += [x] * 10
print b

This answer is better, idea from lenik

a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = [x for x in a for i in range(10)]
print b
Answered By: wa007

You should avoid loops with numpy whenever possible. It kind of defeats the point. Here you can just use repeat():

import numpy as np

a=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
A = np.asarray(a)
B = A.repeat(10)

B:

array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
       4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6,
       6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8,
       8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9])

If want the a nested list, just reshape:

B = A.repeat(10).reshape(-1, 10)

array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
       [3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
       [4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
       [5, 5, 5, 5, 5, 5, 5, 5, 5, 5],
       [6, 6, 6, 6, 6, 6, 6, 6, 6, 6],
       [7, 7, 7, 7, 7, 7, 7, 7, 7, 7],
       [8, 8, 8, 8, 8, 8, 8, 8, 8, 8],
       [9, 9, 9, 9, 9, 9, 9, 9, 9, 9]])
Answered By: Mark

Try this:

a = [*range(10)]
b = []
for i in range(10):
    b.extend([a[i]* 10])
B = np.asarray(b)   
Answered By: ThunderPhoenix

If a were a generic list and not an ordered sequence

In [20]: a = [1, 'a', 3.14159, False, {1:2, 3:4}]                                         

you could do as follows

In [21]: [_ for _ in (zip(*(a for _ in a))) for _ in _]
Out[21]: 
[1,
 1,
 1,
 1,
 1,
 'a',
 'a',
 'a',
 'a',
 'a',
 3.14159,
 3.14159,
 3.14159,
 3.14159,
 3.14159,
 False,
 False,
 False,
 False,
 False,
 {1: 2, 3: 4},
 {1: 2, 3: 4},
 {1: 2, 3: 4},
 {1: 2, 3: 4},
 {1: 2, 3: 4}]
Answered By: gboffi

Answer in a single line: print([item for sublist in [[i]*10 for i in range(1,10)] for item in sublist])

Answered By: Ashish Karn

list1=[]
list2=[]
for i in range (0,10,1):
list1.append(i)
print(list1)
for i in range (0,10,1):
for j in range (0,10,1):
j=i
list2.append(j)
print(list2)

Answered By: Nur Sayed
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.