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

请教一个 Python 处理数据的问题

  •  
  •   NaVient · 2017-08-24 15:32:50 +08:00 · 1404 次点击
    这是一个创建于 2437 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我现在有两组 list,每组 list 中大概都有 1w 个 dict.结构类似这样的[{},{},{},{},{}]

    两组 list 中的 dict 数量是相同的,少部分有差异,不同 list 中的 dict 靠 dict 的 id 来确定匹配 dict.

    我想比较下这两组 list 中对应 dict 的 key 对应的 value 值的差异.

    目前用两个 for 循环来实现感觉方法很笨,效率实在有点低,应该不是最佳实践.

    各位大佬有更好的建议吗?

    byluoluo
        1
    byluoluo  
       2017-08-24 15:37:52 +08:00
    先对两个 list 排序,然后用一个 for 循环就可以了,时间复杂度就是 O(logN)。不知道有没有理解错你的意思。。
    NaVient
        2
    NaVient  
    OP
       2017-08-24 15:43:11 +08:00
    @byluoluo 没有理解错,但是我的数据没有排序的参考位,id 是泛指,真实数据是 case_example_xx+1 这样的 T_T
    topbandit
        3
    topbandit  
       2017-08-24 16:00:37 +08:00
    1w 个 dict,能把一个 list 中的所有 dict 合并成一个 dict 麽?
    NaVient
        4
    NaVient  
    OP
       2017-08-24 16:03:18 +08:00
    @topbandit 不能,每个 dict 都是相似的数据结果,可以理解为一次测试的 1w 个结果
    forkme
        5
    forkme  
       2017-08-24 16:25:52 +08:00
    可以先把[{},{},{},{},{}]做个转换,转换为[‘ id1 ’:{},'id2':{},'id2'{},'id4':{},'id5':{},...] , 然后再用一层 for 遍历比较?
    forkme
        6
    forkme  
       2017-08-24 16:26:33 +08:00   ❤️ 1
    @forkme 错了,是这样{‘ id1 ’:{},'id2':{},'id2'{},'id4':{},'id5':{},...}
    NaVient
        7
    NaVient  
    OP
       2017-08-24 16:33:26 +08:00
    @forkme 这样挺不错的,时间复杂度没有变得更复杂,我去试试,十分感谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5242 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 09:19 · PVG 17:19 · LAX 02:19 · JFK 05:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.