Python 2.7.3 (default, Sep 26 2012, 21:51:14)
>>> ll = [('bear', 1),
... ('is', 1),
... ('in', 1),
... ('gugledarc', 1),
... ('the', 1),
... ('sdpij', 2),
... ('emdf', 2),
... ('sai', 2),
... ('sd', 3),
... ('fuggle', 4),
... ('in', 4),
... ('gugledarc', 4),
... ('df', 4)]
>>> dd = {}
>>> for key, value in ll:
... dd.setdefault(key, []).append(value)
...
>>> dd.items()
[('sai', [2]), ('emdf', [2]), ('df', [4]), ('is', [1]), ('bear', [1]), ('gugledarc', [1, 4]), ('in', [1, 4]), ('the', [1]), ('sdpij', [2]), ('fuggle', [4]), ('sd', [3])]
提案されているように、これは以下を使用した別の実装defaultdict
です:
>>> from collections import defaultdict
>>> dd = defaultdict(list)
>>> for key, value in ll:
... dd[key].append(value)
...
>>> dd.items()
[('sai', [2]), ('emdf', [2]), ('df', [4]), ('is', [1]), ('bear', [1]), ('gugledarc', [1, 4]), ('in', [1, 4]), ('the', [1]), ('sdpij', [2]), ('fuggle', [4]), ('sd', [3])]