将数组列表转换为列表?

时间:2019-04-17 16:07:13

标签: python arrays list type-conversion

我有一个类似这样的数组列表:

  list:
       [[['23456', '23456']], [['459687', '201667'],['769687', '203416']]

如何删除嵌套的[]以获得类似这样的列表列表:

   list:
       [['23456', '23456'], ['459687', '201667'],['769687', '203416']]

有什么主意吗?

1 个答案:

答案 0 :(得分:1)

new_list = []
for sub_list in nested_list:
    if type(sub_list[0]) == list:
        for potential_list in sub_list:
            if type(potential_list) == list:
                new_list.append(potential_list)
    elif type(sub_list[0]) == str:
        new_list.append(sub_list)
    else:
        print(type(sub_list)) # if you get here, you have even more weird nesting than in your example

这将处理您的示例,但不会处理比示例更深的嵌套。如果您需要更深层的嵌套,请创建类似于以下内容的函数,但使用递归