1

I am attempting to flatten a list using:

wd = ['this' , 'is']

np.asarray(list(map(lambda x : list(x) , wd))).flatten()

which returns:

array([['t', 'h', 'i', 's'], ['i', 's']], dtype=object)

when I'm expecting a char array: ['t','h','i','s','i','s']

Is this correct use of flatten?

MSeifert
  • 145,886
  • 38
  • 333
  • 352
blue-sky
  • 51,962
  • 152
  • 427
  • 752

3 Answers3

3

No, this isn't a correct use for numpy.ndarray.flatten.

Two-dimensional NumPy arrays have to be rectangular or they will be cast to object arrays (or it will throw an exception). With object arrays flatten won't work correctly (because it won't flatten the "objects") and rectangular is impossible because your words have different lengths.

When dealing with strings (or arrays of strings) NumPy won't flatten them at all, neither if you create the array, nor when you try to "flatten" it:

>>> import numpy as np
>>> np.array(['fla', 'tten'])
array(['fla', 'tten'], dtype='<U4')
>>> np.array(['fla', 'tten']).flatten()
array(['fla', 'tten'], dtype='<U4')

Fortunately you can simply use "normal" Python features to flatten iterables, just to mention one example:

>>> wd = ['this' , 'is']
>>> [element for sequence in wd for element in sequence]
['t', 'h', 'i', 's', 'i', 's']

You might want to have a look at the following Q+A for more solutions and explanations:

MSeifert
  • 145,886
  • 38
  • 333
  • 352
1

with just a list iteration:

[u for i in np.asarray(list(map(lambda x : list(x) , wd))) for u in i]

gives you this:

['t', 'h', 'i', 's', 'i', 's']

Although, as the comments say, you can just use ''.join() for your specific example, this has the advantage of working for numpy arrays and lists of lists:

test = np.array(range(10)).reshape(2,-1)

[u for i in test for u in i]

returns a flat list:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
sacuL
  • 49,704
  • 8
  • 81
  • 106
0
In[8]: from itertools import chain
In[9]: list(chain.from_iterable(['this' , 'is']))
Out[9]: ['t', 'h', 'i', 's', 'i', 's']
G_M
  • 3,342
  • 1
  • 9
  • 23