V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
scriptB0y
V2EX  ›  Python

清空 Python list 的 100 种方法……

  •  1
     
  •   scriptB0y ·
    laixintao · 2018-03-23 09:52:09 +08:00 · 5170 次点击
    这是一个创建于 2216 天前的主题,其中的信息可能已经有所发展或是发生改变。

    转载自 http://www.pythondoeswhat.com/2018/03/the-zen-of-empty-lists.html

    假如有个 list 叫做 bucket ,将其清空的方法有:

    1. bucket = []
    2. bucket.clear()
    3. del bucket[:]
    4. bucket[:]=[]
    5. bucket[:]={}
    6. bucket *=0
    7. bucket *= -3
    8. bucket *= False
    15 条回复    2018-03-23 16:41:34 +08:00
    infun
        1
    infun  
       2018-03-23 09:54:12 +08:00 via Android   ❤️ 5
    Python 的哲学是啥来着
    scriptB0y
        2
    scriptB0y  
    OP
       2018-03-23 09:54:51 +08:00
    @infun "There should be one-- and preferably only one --obvious way to do it". >_<
    th00000
        3
    th00000  
       2018-03-23 09:57:33 +08:00
    最近看到了阿里云的 redis 开发规范好像有提到这么一嘴
    使用 hscan、sscan、zscan 方式渐进式删除,同时要注意防止 bigkey 过期时间自动删除问题
    效率影响很大
    yangg
        4
    yangg  
       2018-03-23 10:58:31 +08:00
    现在的标题只夸张 10 倍了?
    marcong95
        5
    marcong95  
       2018-03-23 11:08:06 +08:00
    @yangg 方法 7 后面*=任意负整数都可以清空,这么看来是有 2^31 - 1 种方法,加上 lz 说的另外 7 种,那一共是有 2^31 + 6 种了,那就变成夸小了。。
    marcong95
        6
    marcong95  
       2018-03-23 11:08:46 +08:00
    @yangg 负整数是 2^31 个,所以上述数字要加上 1
    msg7086
        7
    msg7086  
       2018-03-23 11:29:00 +08:00
    那么喜欢把玩,为什么不去玩 Ruby 呢。
    shuizhengqi
        8
    shuizhengqi  
       2018-03-23 11:42:59 +08:00
    搞不懂你要干什么,直接 list=[]不好用?
    suhaocong
        9
    suhaocong  
       2018-03-23 11:45:39 +08:00
    同上 list=[]
    sleshep
        10
    sleshep  
       2018-03-23 12:45:10 +08:00
    clear ()

    =[]
    的副作用不一样的
    LokiSharp
        11
    LokiSharp  
       2018-03-23 13:15:39 +08:00
    list=[] 会创建一个新的对象
    ipwx
        12
    ipwx  
       2018-03-23 13:18:21 +08:00 via iPhone
    @shuizhengqi 复用旧 list 的缓冲空间,常数级优化。
    zbl430
        13
    zbl430  
       2018-03-23 15:14:01 +08:00
    clear() python3....
    est
        14
    est  
       2018-03-23 15:24:20 +08:00   ❤️ 1
    其实还好。一共就 4 种方法:

    1. 全部赋一个新的值。 = [] *= 都一样。老的没了被 GC 了
    2. .clear()
    3. del
    4. [:] = 赋值。

    我觉得 用 第二种最好。因为可以实现批量删除。但是似乎 py2 里不支持。
    Icezers
        15
    Icezers  
       2018-03-23 16:41:34 +08:00
    Exception in thread "read v2ex" java.lang.ArrayIndexOutOfBoundsException: 0
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3360 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 12:23 · PVG 20:23 · LAX 05:23 · JFK 08:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.