0

I have a list as shown below:- ( print output)

  port_list=    [array(['51175-80,', '51176-80,'],
  dtype='<U9'), array(['51259-80,', '51260-80,'],
  dtype='<U9'), array(['51096-80,'],
  dtype='<U9'), array(['51079-80,', '51080-80,'],
  dtype='<U9'), array(['51765-80,', '51766-80,'],
  dtype='<U9'), array(['51402-80,', '51403-80,'],
  dtype='<U9'), array(['51235-80,', '51236-80,'],
  dtype='<U9'), array(['51381-80,', '51382-80,'],
  dtype='<U9'), array(['44647-40000,'],
  dtype='<U12')] 

I want to convert this to an numpy array as follows:-

[['51175-80,' '51176-80,' '51259-80,' '51260-80,'...]] 

How can I do this?

Thanks a ton for any help here.

rafaelc
  • 57,686
  • 15
  • 58
  • 82
sunny
  • 643
  • 2
  • 11
  • 29
  • you are looking for `np.hstack(port_list)` or even `np.concatenate(port_list,axis=None)` – Onyambu Sep 09 '18 at 18:45
  • Possible duplicate of [Making a flat list out of list of lists in Python](https://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python) – rafaelc Sep 09 '18 at 18:56
  • 1
    @RafaelC there is an addition of one more axis here, so I tend to believe its really not a duplicate – Onyambu Sep 09 '18 at 18:57
  • 1
    Since some arrays have 2 elements, and others 1, ordinary `nparray(yourlist)` results in a 1d object dtype array. `hstack` can combine the arrays into one 1d array (`U12` dtype). Making it (1,n) just requires a common numpy reshape. But do you really want (1,n) instead of (n,)? – hpaulj Sep 09 '18 at 19:01
  • @Onyambu Fair enough, but it is easily adaptable I believe. – rafaelc Sep 09 '18 at 19:03
  • Thank you all of you for your response. @Onyambu, on using hstack, I get a completely flattened array (1,16) but instead I want to get (1,9) keeping the inner arrays intact minus the dtype=' – sunny Sep 10 '18 at 20:24

0 Answers0