擅长:python、mysql、java
<p>为了收集和分组数据,<a href="https://docs.python.org/2/library/collections.html#collections.defaultdict" rel="nofollow noreferrer">^{<cd1>}</a>似乎是一个很好的选择。请参见下面的示例。你知道吗</p>
<pre><code>from collections import defaultdict
data = [
[1, 1, 1, 'a'],
[1, 1, 1, 'b'],
[1, 1, 2, 'c'],
[2, 1, 1, 'd'],
]
accumulator = defaultdict(list)
for row in data:
# use a tuple of the first three elements as the key
# append the fourth element to a list tracking the values
accumulator[tuple(row[:-1])].append(row[-1])
print accumulator
# defaultdict(<type 'list'>, {(1, 1, 1): ['a', 'b'], (2, 1, 1): ['d'], (1, 1, 2): ['c']})
</code></pre>
<p>将数据转换成本例中使用的表单应该很容易,而获得所需的输出应该正好相反。你知道吗</p>