嵌套列表轉換爲單列表

1.遞歸實現

a = [1,[2,[3],4],5]
def list_more(arg):
    new_list = []
        for i in arg:
            if type(i) is not list:
                new_list.append(i)
        else:
            new_list.extend(list_more(i))
     return new_list

In [65]: list_more(a)
Out[65]: [1, 2, 3, 4, 5]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章