假设我有一个列表列表。
>>> my_list = [[1, 2, 3], [4, 5, 6], [7, 8, [9, 0]]]
是否有办法从该列表中取出所有项目,并使其成为单个未嵌套的列表? 像这样:
>>> break_up(my_list)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
答案 0 :(得分:1)
这使用了递归
def appnd(new_list = [], list_to_append=[]):
for i in list_to_append:
if type(i) == list:
appnd(new_list=new_list, list_to_append=i)
else:
new_list.append(i)
def main():
my_list = [[1, 2, 3], [4, 5, 6], [7, 8, [9, 0]]]
new_list = []
appnd(new_list=new_list, list_to_append=my_list)
print(new_list)
if __name__ == '__main__':
main()