1

我有一个字符串矩阵。如何在前面添加一列,例如

[['a','b'],   ==>   [['e','a','b'],
 ['c','d'],          ['e','c','d'],
 ['a','b'],   ==>    ['e','a','b'],
 ['c','d'],          ['e','c','d'],
 ['a','b'],   ==>    ['e','a','b'],
 ['c','d']]          ['e','c','d']]
4

1 回答 1

1

@Paul 评论中的答案说明了一切。为了完整起见,在此处添加。

In [1]: a = np.tile(np.array([["a","b"], ["c","d"]]), (3,1))

In [2]: a 
Out[2]: array([['a', 'b'],
       ['c', 'd'],
       ['a', 'b'],
       ['c', 'd'],
       ['a', 'b'],
       ['c', 'd']], 
      dtype='<U1')

In [3]: e = np.tile("e", a.shape[0])[None].T # REF: https://stackoverflow.com/a/11885718/155813

In [4]: e 
Out[4]: array([['e'],
       ['e'],
       ['e'],
       ['e'],
       ['e'],
       ['e']], 
      dtype='<U1')

In [5]: np.hstack([e, a]) 
Out[5]: array([['e', 'a', 'b'],
       ['e', 'c', 'd'],
       ['e', 'a', 'b'],
       ['e', 'c', 'd'],
       ['e', 'a', 'b'],
       ['e', 'c', 'd']], 
      dtype='<U1')
于 2013-04-23T03:25:06.273 回答