1

I've a data like this,

data = [array(['a', 'b', 'c']), 
        array([['d', 'e', 'f'], ['g', 'h', 'i']]), 
        array([['j', 'k', 'l'], ['m', 'n', 'o'], ['p', 'q', 'r']])]

I want to join values in inner list sequentially. This is the desired output that I need.

[['a', 'b', 'c'], ['d g', 'e h', 'f i'], ['j m p', 'k n q', 'l o r']]

I tried using using multiple loops and join but it's not giving me required output.

for i in data:
    for j in i:
        print(" ".join(j))

I'm not really sure on how to achieve this? Need an efficient and faster approach because my original data is really large.

10
  • That's not a NumPy array, though? You're not going to get any performance improvement if all you do is loop over a NumPy array, by the way. Commented Feb 16, 2020 at 20:18
  • @AMC actually it's a numpy array but I posted it as normal list. Commented Feb 16, 2020 at 20:21
  • Why...? What are you using the arrays for? Commented Feb 16, 2020 at 20:21
  • @AMC I was given this data. I didn't prepare it this way. What would you suggest? I have large data in which I want this desired output. It's a list of numpy arrays. Commented Feb 16, 2020 at 20:22
  • Is it a list of NumPy ndarrays, or is it a multidimensional ndarray? I'm trying to convert the data you shared properly. Commented Feb 16, 2020 at 20:23

3 Answers 3

3
In [300]: [np.array([' '.join(ij) for ij in zip(*np.atleast_2d(row))]) for row in data]        
Out[300]: 
[array(['a', 'b', 'c'], dtype='<U1'),
 array(['d g', 'e h', 'f i'], dtype='<U3'),
 array(['j m p', 'k n q', 'l o r'], dtype='<U5')]

The first array is 1d, thus requiring the atleast_2d for consistency. Most the rest is just iterative application of string join.

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

1 Comment

very nice use of np.atleast_2d :) +1
2

Try:

res=list(map(lambda x: list(map(' '.join, zip(*x))) if isinstance(x[0], list) else x, data))

Outputs:

[['a', 'b', 'c'], ['d g', 'e h', 'f i'], ['j m p', 'k n q', 'l o r']]

4 Comments

@GrzegorzSkibinski Thank you but I'm getting this error TypeError: sequence item 0: expected str instance, Timestamp found
Try: res=list(map(lambda x: list(map(lambda z: ' '.join([str(el) for el in z]), zip(*x))) if isinstance(x[0], list) else x, data))
@GrzegorzSkibinski I tried but it's not working. I mean the error is gone but it returns input data as output. It's not joining
Ok, since it's not a list, but numpy try: res=list(map(lambda x: list(map(lambda z: ' '.join([str(el) for el in z]), zip(*x))) if isinstance(x[0], np.ndarray) else x, data))
1

Since you say there are also timestamp values, I cast to string for every sub-array.

out = [[' '.join(tup) for tup in zip(*arr.astype(str))] if arr.ndim > 1 else arr.tolist() 
               for arr in data]

Out[89]: [['a', 'b', 'c'], ['d g', 'e h', 'f i'], ['j m p', 'k n q', 'l o r']]

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.