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

列表内字典组合问题 求助

  •  
  •   334862132 · 2019-07-15 11:52:25 +08:00 · 1578 次点击
    这是一个创建于 1719 天前的主题,其中的信息可能已经有所发展或是发生改变。
    list1 = [{'id':1,'name':'one'},{'id':2,'name':'two'}]
    list2 = [{'id':1,'age':18},{'id':2,'age':17}]

    list3 = [{'id':1,'name':'one','age':18},{'id':2,'name':'two','age':17}]
    我现在想通过 list1 和 list2 获取 list3 最好的办法是什么
    有没有像 mysql 一样的连接功能?
    感觉用遍历挨个拼接很蠢,求解决办法
    10 条回复    2019-07-15 15:42:39 +08:00
    Takamine
        1
    Takamine  
       2019-07-15 12:08:11 +08:00   ❤️ 2
    # 我记得世界上最好的语言有处理这个的函数,不知道 Py 有没有。
    # 这个办法好像也很蠢:doge:。
    ```python
    list3 = []
    for i, j in zip(list1, list2):
    i.update(j)
    list3.append(i)
    ```
    334862132
        2
    334862132  
    OP
       2019-07-15 12:12:14 +08:00
    @Takamine 听朋友说 pandas 里面好像有处理这种数据的方法 这在找。。。。
    rrfeng
        3
    rrfeng  
       2019-07-15 12:13:33 +08:00 via Android   ❤️ 1
    按 id ?那把 id 当 key 扔字典里,遍历一遍 update 就行了呗
    rrfeng
        4
    rrfeng  
       2019-07-15 12:15:38 +08:00 via Android
    然后再整成 list。或者直接用 list,id 当索引(范围可控的话,不可控就存个 id 到 list 的索引)

    不会比 pandas 差。
    starsriver
        5
    starsriver  
       2019-07-15 12:40:38 +08:00 via Android   ❤️ 1
    定义 namebuff 和 agebuff 一维数组,用 id 作为序号索引。更新数据只需要把提取的 list 组合进去就好
    txy3000
        6
    txy3000  
       2019-07-15 12:44:53 +08:00   ❤️ 1
    直接上代码
    def func(l): return {e['id']: e for e in l}
    l1 = [{'id': 1, 'name': 'one'}, {'id': 2, 'name': 'two'}]
    l2 = [{'id': 1, 'age': 18}, {'id': 2, 'age': 17}]

    d1, d2 = func(l1), func(l2)

    l3 = list(dict(d1[i].items() | d2[i].items())
    for i in d1.keys() & d2.keys())
    lance86
        7
    lance86  
       2019-07-15 12:54:24 +08:00   ❤️ 1
    用列表推导式?不过其实也是嵌套的 for 循环,,
    [{'id': item1['id'], 'name': item1['name'], 'age': item2['age']} for item1 in list1 for item2 in list2 if item1['id'] == item2['id']]
    goofool
        8
    goofool  
       2019-07-15 13:57:39 +08:00
    print(list1, list2)
    list3 = copy.copy(list1)
    [x[0].update(x[1]) for x in zip(list3, list2)]
    print(list3)
    rrfeng
        9
    rrfeng  
       2019-07-15 14:13:10 +08:00
    result = []
    index = {}
    c = 0

    for i in list1+list2:
    index[i['id']] = c
    if not resut[c]:
    result[c] = i
    else:
    result[c].update(i)
    c++
    princelai
        10
    princelai  
       2019-07-15 15:42:39 +08:00   ❤️ 1
    ```
    pd.merge(left=pd.DataFrame(list1), right=pd.DataFrame(list2),left_on='id', right_on='id').to_dict('records')
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5414 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 07:52 · PVG 15:52 · LAX 00:52 · JFK 03:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.