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
Phishion
V2EX  ›  Python

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

  •  
  •   Phishion · 2022-04-06 18:43:58 +08:00 · 1585 次点击
    这是一个创建于 760 天前的主题,其中的信息可能已经有所发展或是发生改变。

    大概代码如下,其中主列表 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
       2022-04-06 18:56:37 +08:00
    行了,当我没问,我用列表解析遍历只要个位数 ms ,不知道为什么列表解析快那么多
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5207 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 09:46 · PVG 17:46 · LAX 02:46 · JFK 05:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.