1
a = [0,0,1,1,0,2]
b = ['a','b','c','d','e','f']

ab对应的,按照a的分类取出b的元素,
就是想得到矩阵
[ ['a','b','e'], ['c','d'], ['f'] ]
用几个遍历肯定是可以处理的
想要更优雅的方法
谢谢

1个回答

0
a = [0,0,1,1,0,2]
b = ['a','b','c','d','e','f']

from collections import defaultdict
map = defaultdict(list)
for key, value in zip(a, b): map[key].append(value)
print(map.values())

撰写答案