python中list可以嵌套吗

2025-12-02 0 32,946

python中的列表是可以嵌套的。将嵌套的list遍历并输出是很常见的需求。以下通过两种方法达到目的

def nested_list(list_raw,result):
    for item in list_raw:
        if isinstance(item, list):
            nested_list(item,result)
        else:
            result.append(item)
    return  result   
def flatten_list(nested):
    if isinstance(nested, list):
        for sublist in nested:
            for item in flatten_list(sublist):
                yield item
    else:
        yield nested
def main():   
    list_raw = ["a",["b","c",["d"]]]
    result = []
    print "nested_list is:  ",nested_list(list_raw,result)
    print "flatten_list is: ",list(flatten_list(list_raw))
main()

让代码run起来,输出为:

nested_list is:   ['a', 'b', 'c', 'd']
flatten_list is:  ['a', 'b', 'c', 'd']

nested_list方法采用递归的方式,如果item是list类型,继续递归调用自身。如果不是,将item加入结果列表中即可。

flatten_list方法则是采用生成器的方式,本质上也是递归的思路。

推荐学习《python教程》

2.两层嵌套list去重

list里面套了一层list,需要去重,并在生成一个去重的list。请看代码:

def dup_remove_set(list_raw):
    result = set()
    for sublist in list_raw:
        item = set(sublist)
        result = result.union(item)
    return list(result)
def main():  
    list_dup = [[1,2,3],[1,2,4,5],[5,6,7]]
    print dup_remove_set(list_dup)

让代码run起来:

[1, 2, 3, 4, 5, 6, 7]

基本思路:将每一个子list转为set,然后求并集,即可。

3.多重嵌套去重

def dup_remove(list_raw,result):
    for item in list_raw:
        if isinstance(item, list):
            dup_remove(item,result)
        else:
            result.add(item)
    return  list(result)
def main():   
    list_raw = ["a",["b","c",["d","a","b"]]]
    result = set()
    print "dup_remove is:  ",dup_remove(list_raw,result)

让代码run起来:

dup_remove is:   ['a', 'c', 'b', 'd']

基本思路与之前遍历嵌套list的思路差不多,唯一的区别就是之前result是一个list,而要去重的话用result是一个set,保证最后的结果为去重的结果。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:以上部本文内容由互联网用户自发贡献,本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。投诉邮箱:3758217903@qq.com

ZhiUp资源网 常见问题 python中list可以嵌套吗 https://www.zhiup.top/7935.html

相关