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

Python 两个大列表如何对比相同内容并合并?

  •  
  •   webing · 2018-06-03 01:37:08 +08:00 · 1969 次点击
    这是一个创建于 2154 天前的主题,其中的信息可能已经有所发展或是发生改变。

    题外话: 在实际中,A 表有 1W 行,B 表只有 4K 行。 当,迭代 A 时,B 迭代一次,然后再从 A 到 B 依次迭代,就会导致,有的行数没有匹配上。 所以,只有,迭代出 A 的中的一行,就得对比 B 中的全部行数,这样才是有效的。

    需求:

    通过 A,匹配 B,把部分相同内容的两个列表合并(这里要匹配列表前边的数字 11,22,33,44 通过数字来合并其他内容,11 不能和 22 的数据合并,11 要和 11 的数据合并。),要求 4 个列表都要匹配完。 如:

    A 的 ['11', 'wqdqwdwd', '允许撒大声地']
    匹配到
    B 的 ['11', 'ikluhkmug', '允许 dwadwad']
    把这两个表合并。
    这里要匹配列表前边的数字 11,22,33,44 通过数字来合并其他内容,11 不能和 22 的数据合并,11 要和 11 的数据合并。
    

    使用字典会打乱顺序,所以只能采用列表。

    大列表 A:

    [
    ['11', 'wqdqwdwd', '允许撒大声地'],
    ['22', 'ewrdwewd', '允许打发斯蒂'],
    ['33', 'hghfgfgh', '允许撒大我地'],
    ['44', 'gfdgdgrg', '允许伟大无多']
    ]
    

    大列表 B:

    [
    ['22', 'jhgfghfgh', '允许 dawdadd'],
    ['33', 'hfghgffgh', '允许 wadawwa'],
    ['44', 'jyjygjyyd', '允许 dwadwad'],
    ['11', 'ikluhkmug', '允许 dwadwad']
    ]
    

    已经进行的尝试:(都失败了)

    1、for 迭代大列表 A,B 放进队列,但是 for 和 queue 队列,for 只迭代了一次。

    for x in A:
       while not q.empty():
           wait_list = q.get()
           if x[0] in wait_list[0]
    
    
    这里的 for x in A:只迭代一次就不迭代了。按照正常情况,A 迭代 4 次才对。
    
    id_a = []
    for i,j in enumerate(a):
        id_a.append((i,j[0]))
    print(id_a)
     
    id_b = []
    for i,j in enumerate(b):
        id_b.append((i, j[0]))
    print(id_b)
     
     
    for i in id_a:
        for j,k in enumerate(id_b):
            if i[1] == k[1]:
                a[i[0]].extend(b[k[0]])
     
    print(a)
     
    for i in a:
        del i[3]
     
    print(a)
    这里其实就是两个 for 迭代,直接判断,只不过写的复杂化了。
    

    2、把 A 和 B 放进不同的队列,A 队列也只是获取一次内容就不获取了。跟上边的 for 循环一样。

    2 条回复    2018-06-12 07:58:42 +08:00
    copie
        1
    copie  
       2018-06-03 10:41:19 +08:00
    a = [
    ['11', 'wqdqwdwd', '允许撒大声地'],
    ['22', 'ewrdwewd', '允许打发斯蒂'],
    ['33', 'hghfgfgh', '允许撒大我地'],
    ['44', 'gfdgdgrg', '允许伟大无多']
    ]

    b = [
    ['22', 'jhgfghfgh', '允许 dawdadd'],
    ['33', 'hfghgffgh', '允许 wadawwa'],
    ['44', 'jyjygjyyd', '允许 dwadwad'],
    ['11', 'ikluhkmug', '允许 dwadwad']
    ]

    c = {x[0]: x for x in b}
    d = []
    for line in a:
    if line[0] in c:
    c[line[0]] += line[1:]
    d.append(c[line[0]]+line[1:])

    from pprint import pprint
    pprint("add_list.py:21")
    pprint(c)
    ywh123456
        2
    ywh123456  
       2018-06-12 07:58:42 +08:00
    from collections import defaultdict
    a = [
    ['11', 'wqdqwdwd', '允许撒大声地'],
    ['22', 'ewrdwewd', '允许打发斯蒂'],
    ['33', 'hghfgfgh', '允许撒大我地'],
    ['44', 'gfdgdgrg', '允许伟大无多']
    ]

    b = [
    ['22', 'jhgfghfgh', '允许 dawdadd'],
    ['33', 'hfghgffgh', '允许 wadawwa'],
    ['44', 'jyjygjyyd', '允许 dwadwad'],
    ['11', 'ikluhkmug', '允许 dwadwad']
    ]

    c = defaultdict(list)
    for tmp in a + b:
    c[tmp[0]].append(tmp)

    #输出
    for key in c.keys():
    print("{} has {}".format(key,c[key]))
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1008 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 19:38 · PVG 03:38 · LAX 12:38 · JFK 15:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.