隔江千里
raw = [# <date> <key> <val1> <val2> ['2020/10/07', 'AAA123', '19.24', '22.00'], ['2020/11/17', 'BBB123', '23.59', '00.00'], ['2020/14/67', 'AAA123', '08.00', '16.00'],](稍微编辑您的示例以具有重复的密钥)听起来你希望你的输出是这样的:{key: [info, ...], ...}我们可以这样做:from collections import defaultdict, namedtupleData = namedtuple('Data', ('date', 'val1', 'val2'))res = defaultdict(list)for date, key, val1, val2 in raw: data = Data(date, val1, val2) res[key].append(data)res = dict(res) # Strip defaultdict behavior然后我们可以看到import pprintpprint.pprint(res)给我们一个与期望类似的结果:{'AAA123': [Data(date='2020/10/07', val1='19.24', val2='22.00'), Data(date='2020/14/67', val1='08.00', val2='16.00')], 'BBB123': [Data(date='2020/11/17', val1='23.59', val2='00.00')]}