>>> res = [[None,'A','B'],[19980228, 'd1', 't1'],[19980302, 'd2', 't2'],[19980303, 'd3', 't3']]
>>> {a:[(r[0], r[i]) for r in res[1:]] for i, a in enumerate(res[0]) if a}
{'A': [(19980228, 'd1'), (19980302, 'd2'), (19980303, 'd3')], 'B': [(19980228, 't1'), (19980302, 't2'), (19980303, 't3')]}
注意:正如@shuzOMGchen 指出的那样,这需要在 python 2.7 和 3.0 中添加的“字典理解”,因此如果您使用的是较早版本,则必须稍微更改代码。这里没有使用字典理解(这很丑,我只是试图从上面复制我的逻辑)
>>> res = [[None,'A','B'],[19980228, 'd1', 't1'],[19980302, 'd2', 't2'],[19980303, 'd3', 't3']]
>>> d = {}
>>> for i, a in enumerate(res[0]):
... if a:
... d[a] = [(r[0], r[i]) for r in res[1:]]
...
>>> d
{'A': [(19980228, 'd1'), (19980302, 'd2'), (19980303, 'd3')], 'B': [(19980228, 't1'), (19980302, 't2'), (19980303, 't3')]}