如何把嵌套的python list转成一个一维的python list

2025-04-02 13:31:19
推荐回答(1个)
回答1:

# coding: utf-8

def some_list_to_one(the_list):
result = list()
for d in the_list:
if type(d) == list:
result += some_list_to_one(d)
else:
result.append(d)
return result

if __name__ == "__main__":
a = [1, [2, [5, [7, 8], 6], 3], 4]
print some_list_to_one(a)