0

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 ]
3
  • 1
    Please format your code Commented Jul 3, 2020 at 11:43
  • 4
    Have a look at the numpy.repeat function; docs here. Commented Jul 3, 2020 at 11:47
  • Best answer thanks!!! Commented Jul 3, 2020 at 12:35

9 Answers 9

2

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Actually by using a nother vector as A I obtain every 9 value a 0. I cannot understand why
I edited my answer so it can be more scalable. I hope it will work now
1

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

3 Comments

The double for loop seems a bit over kill (harder to understand) when you can instead use [[a]*10 for a in A]
@JakobVinkas you'll get list of lists, not a simple list from your code. try running it in python
Oh you are correct, I wrote my answer before the question was edited to clearly display the structure of the B array, my bad.
1

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

Comments

0

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

2 Comments

My answer is not the best one, but in this case a referes to a in A making such that a will take iterating values (all of the values) of A from start to end of the array
Ok I understood it is the element of the array. But, now I get the following results: [[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]] Is it possible to compact everything in one array? In this way: [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]
0

Try this:

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

Comments

0
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

3 Comments

Hi, how would be possible to avoid the double square brakets?
I don't think it can avoid.
The answer [x for i in a for x in [i] * 10] from lenik is nice.
0

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

Comments

0

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

Comments

-1
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)

1 Comment

Please add some explanations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.