-3

各タプルの最初の項目に参加する必要があるタプルのリスト (以下を参照) があります。したがって、結果は (word, list(numbers)) タプルのリストになります。

In [351]: word_docid_pairs
Out[351]: 
[('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)]
4

1 に答える 1

1
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])]
于 2012-12-30T07:41:15.913 に答える