zoukankan      html  css  js  c++  java
  • 压平列表

    碾平列表是个很好玩的函数。比如你有个嗷嗷恶心的列表:

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

    你想把它变成正常一点的

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

    要怎么办呢?

    老实说,很久不接触这种东西,我已经早忘了当初是怎么写的了,憋了半天没写出来,后来参考了 http://www.cnblogs.com/c-hy/archive/2012/09/21/2696703.html 才回忆起来。

    然后,着重介绍三种方法:

    1、使用sum和map,感觉玩的比较炫,大家来感受一下:

    from collections import Iterable
    
    
    def flatten(x):
        if isinstance(x, Iterable) and not isinstance(x, (str, bytes)):
            return sum(map(flatten, x), [])
        else:
            return [x]
    
    
    lst = [1, 2, [3, [4], [5, 6], 7], 8]
    print(flatten(lst))

    刚才那个网页中说,国外某论坛的大神写了一个匿名函数

    flat=lambda L: sum(map(flat,L),[]) if isinstance(L,list) else [L]

    基本上是一个意思的。

    2、使用yield。这个是在《Python Cookbook》中介绍的一种方法

    from collections import Iterable
    
    
    def flatten(items, ignore_types=(str, bytes)):
        for x in items:
            if isinstance(x, Iterable) and not isinstance(x, ignore_types):
                yield from flatten(x)
            else:
                yield x
    
    
    items = [[1, 2], [3, [4], [5, 6], 7], 8]
    # Produces 1 2 3 4 5 6 7 8
    for x in flatten(items):
        print(x)

    yield from 比较巧妙。不过,这里有个限制,yield from是python3才有的,如果是在python2中使用,需要这么写:

    from collections import Iterable
    
    
    def flatten(items, ignore_types=(str, bytes)):
        for x in items:
            if isinstance(x, Iterable) and not isinstance(x, ignore_types):
                for i in flatten(x):
                    yield i
            else:
                yield x
    
    
    items = [[1, 2], [3, [4], [5, 6], 7], 8]
    # Produces 1 2 3 4 5 6 7 8
    for x in flatten(items):
        print(x)

    2和3通用。不得不说yield真是个好东西。

    3、Tkinter自带函数_flatten。不说话,直接上代码

    from Tkinter import _flatten
    lst = [1, 2, [3, [4], [5, 6], 7], 8]
    print(_flatten(lst))

    这个就比较可怕了。不过Tkinter这东西,据说只有windows才有,linux没有。mac不太清楚。

    当然,其他方法也是有的,比如

    def flatten(x):
        for i in x:
            if isinstance(i, Iterable) and not isinstance(i, (str, bytes)):
                flatten(i)
            else:
                new_lst.append(i)
    
    
    new_lst = []
    lst = [1, 2, [3, [4], [5, 6], 7], 8]
    flatten(lst)
    print(new_lst)

    (感谢群友提供)思路比较简单,代码也很清晰,只是全局列表到底还是感觉有一点点耍赖。

    当然,要说耍赖,这里还有更耍赖的,比如

    lst = [1, 2, [3, [4], [5, 6], 7], 8]
    print(list("[" + str(lst).replace("[", "").replace("]", "") + "]"))

    还有

    import re
    lst = [1, 2, [3, [4], [5, 6], 7], 8]
    print map(int, re.findall('d+', str(lst)))

    哈哈,很特定的环境下可以使用,比如第一个列表元素中不能含有"["和"]",第二个更是只能包含整数。不过,同学们的脑洞还是比较大的。

    记录一下,留着玩。

  • 相关阅读:
    java类加载全过程
    pyAggr3g470r 3.6 发布,新闻聚合器
    fourinone分布式协调设计解析
    修改openJDK7的javac,使得java支持单引号字符串
    SabreDAV 1.8.0 发布,集成 WebDAV 系统
    openSUSE 12.3 里程碑 1 发布
    GroupOffice 4.0.123 发布
    nagios总结与基本配置模板
    Zorin OS 6.1 发布,基于Ubuntu的Linux
    Ehcache 2.6.2 发布,Java 缓存框架
  • 原文地址:https://www.cnblogs.com/anpengapple/p/7133571.html
Copyright © 2011-2022 走看看