Python merge lists by common element

前端 未结 6 1518
梦毁少年i
梦毁少年i 2021-01-26 09:24

Im trying to merge two lists that have a common thing between them (in that case is a the id parameter). I have something like this:

list1=[(id1,host1

6条回答
  •  余生分开走
    2021-01-26 09:50

    1. use collections.defaultdict to map id->ip
    2. then map id -> ip
    >>> d = defaultdict(set)
    >>> d['id'].add('host1')
    >>> d['id'].add('host2')
    >>> d['id'].add('host1')
    >>> d
    defaultdict(, {'id': set(['host2', 'host1'])})
    

提交回复
热议问题