由于两种情况之间的不一致,您需要的输出看起来有点奇怪。您可以简单地更改此示例以获得所需的任何输出,但是:lists = [
[u'a', 11, u'P'],
[u'a', 11, u'A'],
[u'b', 2, u'P'],
[u'c', 1, u'P'],
[u'c', 2, u'P'],
[u'd', 1, u'P'],
[u'e', 3, u'P'],
[u'f', 2, u'P'],
[u'a', 1, u'P'],
[u'a', 2, u'P'],
[u'b', 1, u'P'],
[u'b', 11, u'P']]
# Each key in this dictionary will be one of the first elements
# from the lists shown above. The values will be dictionaries
# mapping a letter (one of the third elements in each list) to
# their total count (i.e. the sum of the second elements matching
# the other two columns)
from collections import defaultdict</