How to flatten a hetrogenous list of list into a single list in python?

前端 未结 11 1696
暖寄归人
暖寄归人 2020-12-01 22:59

I have a list of objects where objects can be lists or scalars. I want an flattened list with only scalars. Eg:

L = [35,53,[525,6743],64,63,[743,754,757]]
ou         


        
11条回答
  •  醉梦人生
    2020-12-01 23:15

    it could be done neatly in one line using numpy

    import numpy as np
    np.hstack(l)
    

    you end up with an ndarray

    array([  35,   53,  525, 6743,   64,   63,  743,  754,  757])
    

提交回复
热议问题