我有一个字符串列表s = ['a', 'b', 'c']
我想堆叠得到s = ['a','a','a','b','b','b','c','c','c']
我尝试过vstack,然后重塑但得到s = ['a', 'b', 'c','a', 'b', 'c','a', 'b', 'c']
除了for循环以外,还有其他方法吗?
代码如下:
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 ='
答案 0 :(得分: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']
答案 1 :(得分:0)
s = ['a', 'b', 'c']
print ([i for i in s for _ in range(3)])
输出:
['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c']