Jesh Kundem - 2 months ago 11x

Python Question

This is my simple of piece of code

Everything is a numpy array. I welcome manipulation using lists too.

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

b = [3,2,2,2,8]

c = ['test1', 'test2', 'test3','test4','test5']

expected Outcome:

`d = [ 1, 2, 3, 4, 5;`

3, 2, 2, 2, 8;

'test1','test2', 'test3', 'test4','test5' ]

OR

`d = [ 1 3 'test1';`

2 2 'test2';

3 2 'test3';

4 2 'test4';

5 8 'test5']

Answer

Adam's answer using `numpy.concat`

is also correct, but in terms of specifying the exact shape you are expecting — rows stacked vertically — you'll want to look at numpy.vstack:

```
>>> import numpy as np
>>> np.vstack([a, b, c])
array([['1', '2', '3', '4', '5'],
['3', '2', '2', '2', '8'],
['test1', 'test2', 'test3', 'test4', 'test5']],
dtype='<U21')
```

There's a catch here either way you do it: since your separate arrays (`int64`

, `int64`

, `<U5`

) are all being put together, *the new array will automatically use the least restrictive type*, which in this case is the unicode type.

See also: `numpy.hstack`

.

Source (Stackoverflow)

Comments