Convert multi-dimensional list to a 1D list in Python

前端 未结 7 667
萌比男神i
萌比男神i 2020-12-05 00:14

A multidimensional list like l=[[1,2],[3,4]] could be converted to a 1D one by doing sum(l,[]). Can anybody please explain how that happens?

<
相关标签:
7条回答
  • 2020-12-05 00:55

    I've written this function:

    def make_array_single_dimension(l):
        l2 = []
    
        for x in l:
            if type(x).__name__ == "list":
                l2 += make_array_single_dimension(x)
            else:
                l2.append(x)
    
        return l2
    

    It works as well!

    0 讨论(0)
提交回复
热议问题