1

I have a numpy array :

a= [1,2,3,4,5,6]

I need to do some sort of string multiplication on this array as follows :

2*string(a) = [1,1,2,2,3,3,4,4,5,5,6,6]

Is there any way to do this in numpy?

Thanks

cs95
  • 379,657
  • 97
  • 704
  • 746
David
  • 129
  • 1
  • 11

2 Answers2

2

You could use np.repeat:

import numpy as np

a= [1,2,3,4,5,6]
result = np.repeat(a, 2)

print(result)

Output

[1 1 2 2 3 3 4 4 5 5 6 6]
Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76
1

Use numpy repeat:

a = np.array([1,2,3,4,5,6])
print(np.repeat(a,2))

Which gives:

[1 1 2 2 3 3 4 4 5 5 6 6]
Simon
  • 9,762
  • 15
  • 62
  • 119