Combining Dictionaries Of Lists In Python

前端 未结 5 941
独厮守ぢ
独厮守ぢ 2020-11-27 19:20

I have a very large collection of (p, q) tuples that I would like to convert into a dictionary of lists where the first item in each tuple is a key that indexes a list that

5条回答
  •  粉色の甜心
    2020-11-27 19:34

    Here is the iterator style of doing it

    >>> mylist=[(1, 2), (1, 3), (2, 3)]
    >>> from itertools import groupby
    >>> from operator import itemgetter
    >>> mylist=[(1, 2), (1, 3), (2, 3)]
    >>> groupby(mylist,itemgetter(0))
    
    >>> list(_)
    [(1, ), (2, )]
    

提交回复
热议问题