Splitting dict by value of one of the keys

后端 未结 4 835
隐瞒了意图╮
隐瞒了意图╮ 2020-12-19 19:45

I\'ve got a dictionary with data of the same length (but different types), something like:

data = {
    \"id\": [1,1,2,2,1,2,1,2], 
    \"info\": [\"info1\",         


        
4条回答
  •  渐次进展
    2020-12-19 20:12

    >>> from collections import defaultdict
    >>> res = defaultdict(list)
    >>> for ID,info in zip(data["id"],data["info"]):
        res[ID].append(info)
    
    
    >>> res
    defaultdict(, {1: ['info1', 'info2', 'info5', 'info7'], 2: ['info3', 'info4', 'info6', 'info8']})
    >>> 
    

提交回复
热议问题