-10

I have a list of string s = ['a', 'b', 'c']

I want to to get s = ['a','a','a','b','b','b','c','c','c']

I've tried vstack, then reshape but got s = ['a', 'b', 'c','a', 'b', 'c','a', 'b', 'c']

Is there any way to do that other than for loop?

here is the code:

a = ["a","b","c"]
a = np.array(a)
b = np.vstack((a,a,a))
b.reshape(-1)

array(['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c'], dtype='<U1')

Ha Bom
  • 2,787
  • 3
  • 15
  • 29

2 Answers2

2
>>> orig = ['a', 'b', 'c']
>>> n = 3
>>> s = sum(map(lambda x:[x]*n, orig), [])
>>> s
['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c'] 
0
s = ['a', 'b', 'c']
print ([i for i in s for _ in range(3)])

output:

['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c']
ncica
  • 7,015
  • 1
  • 15
  • 37