python的list展开方法


def
flatten(input_list): #遍历原始列表,对于列表内的元素,如果非列表就存入输出列表中,如果是列表元素,则进入改列表元素并查看列表内元素类型” output_list = [] while True: if input_list == []: break for index, i in enumerate(input_list): if type(i)== list: input_list = i + input_list[index+1:] break else: output_list.append(i) input_list.pop(index) break return output_list a = [ 1, 2, [3, 4, [5, 6, [7, 8, [9, [10, ['end'] ] ] ] ] ] ] flatten(a)

通过网络上找 python 的 list展开方法,发现并无更简单优秀的方案,最优方法如上:

输出:

原文地址:https://www.cnblogs.com/andylhc/p/14261046.html