Divide Python lists into lists

Asked 2 years ago, Updated 2 years ago, 57 views

arr = [['a',' b', 'c'], ['b' ,'c' ,'d'], ['d', 'f', 'a']] 
brr = ['a',' b', 'c', 'b' ,'c' ,'d', 'd', 'f', 'a'] 

I'd like to change #listarr to brr. If you have any related methods or methods, please help me.

python list

2022-09-22 18:34

1 Answers

This function is commonly referred to as flatten.

You can use a library like numpy https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.flatten.html

>>> a = np.array([[1,2], [3,4]])
>>> a.flatten()
array([1, 2, 3, 4])
>>> a.flatten('F')
array([1, 3, 2, 4])

It's simple to implement with lambda.

flatten = lambda l: [item for sublist in l for item in sublist]


2022-09-22 18:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.