1

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']
2
  • Are you interested in a particular string display, or a particular data structure (and behavior)? There is a real difference. A ; is part of a display, not a data structure. Commented Aug 23, 2016 at 16:48
  • I am ok with any display as long as I can access the values whenever I want. Thank you or pointing about semicolon. Commented Aug 23, 2016 at 16:57

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Could you tell me what do you mean by unicode type? When i print them I am printing them as strings.
This answer used Python3 where unicode is the default string type.
0

Check out the concat method.

>>> a = np.array([[1, 2], [3, 4]])
>>> b = np.array([[5, 6]])
>>> np.concatenate((a, b), axis=0)
array([[1, 2],
       [3, 4],
       [5, 6]])

4 Comments

Yes I checked it, I dont want the brackets in the final 3D array. And I am also facing problems with my c being a string. I believe it also says - that only length one arrays can be vonverted to python scalars - No clue what that means
The brackets are just there for printing purposes; it's a 3x2 matrix, no?
Should not it look something like this ? [ 1, 2 ; 3,4 ; 5,6]. With semicolumn indicating difference between two rows. I believe if I have the square brackets, that particular row will have its own features - May be I am wrong - but thats what my understanding is
numpy does not use ; to divide or display its dimensions. It's normal 2d display looks a lot like a list of lists.
0

Your a,b,c are lists; you'd have to use np.array([1,2,3]) to get an array, and it will display as [1 2 3] (without the commas).

Simply creating a new list from those lists produces a list of lists

In [565]: d=[a,b,c]
In [566]: d
Out[566]: 
[[1, 2, 3, 4, 5],
 [3, 2, 2, 2, 8],
 ['test1', 'test2', 'test3', 'test4', 'test5']]

and simply concatenating the lists produces one longer one

In [567]: a+b+c
Out[567]: [1, 2, 3, 4, 5, 3, 2, 2, 2, 8, 'test1', 'test2', 'test3', 'test4', 'test5']

numpy arrays have problems containing both numbers and strings. You have to make a 'structured array'.

The easiest way to combine these into one array is with a fromarrays utility function:

In [561]: x=np.rec.fromarrays(([1,2,3],[3,2,2],['test1','test2','test3']))
In [562]: x['f0']
Out[562]: array([1, 2, 3])
In [563]: x['f2']
Out[563]: 
array(['test1', 'test2', 'test3'], 
      dtype='<U5')

In [568]: x
Out[568]: 
rec.array([(1, 3, 'test1'), (2, 2, 'test2'), (3, 2, 'test3')], 
          dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<U5')])

or with a little editing of the display:

In [569]: print(x)
[(1, 3, 'test1') 
 (2, 2, 'test2') 
 (3, 2, 'test3')]

This is not a 2d array; it's 1d (here 3 elements) with 3 fields.

Perhaps the easiest way to format this array in a way that looks like your spec would be with a csv writer:

In [570]: np.savetxt('x.txt',x,fmt='%d  %d  %s;')
In [571]: cat x.txt     # shell command to display the file
1  3  test1;
2  2  test2;
3  2  test3;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.