推荐学习书目
› 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
Phishion
V2EX  ›  Python

请问有什么高效的方法,在保持顺序的前提下,做差集计算?

  •  
  •   Phishion · Apr 6, 2022 · 2331 views
    This topic created in 1646 days ago, the information mentioned may be changed or developed.

    大概代码如下,其中主列表 full_queue 有 5000 个元素,做差集的列表有 50 个左右,计算下来耗时大概 100 多 ms ,有什么方法或者第三方包可以优化这部分的执行时间?

    from datetime import datetime
    
    process_start = datetime.now().timestamp()
    
    full_queue = [4878214304830001, 4878214304830002, 4878214304830003, 4878214304830004, ...]
    diff_queue = [4878214548209665, 4878214308487169, 4878265720848385, ...]
    format_list = list(set(full_queue)-set(diff_queue))
    format_list.sort(key=full_queue.index)
    
    print(format_list[:5])
    process_elapsed = round(datetime.now().timestamp() - process_start, 4) * 1000
    print(process_elapsed)
    
    Phishion
        1
    Phishion  
    OP
       Apr 6, 2022
    行了,当我没问,我用列表解析遍历只要个位数 ms ,不知道为什么列表解析快那么多
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   897 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 20:54 · PVG 04:54 · LAX 13:54 · JFK 16:54
    ♥ Do have faith in what you're doing.