-
暮色呼如
用于zip将您的数组转置为[('a', 1, 4, 7), ...]; 弹出第一个元素作为键,将其余元素列为值。arr = [['a','b','c'],[1,2,3],[4,5,6],[7,8,9]][{ e[0]: list(e[1:])} for e in zip(*arr)]# => [{'a': [1, 4, 7]}, {'b': [2, 5, 8]}, {'c': [3, 6, 9]}]
-
jeck猫
使用带有序列解包的列表推导式:L = [['a','b','c'],[1,2,3],[4,5,6],[7,8,9]]res = [{names: nums} for names, *nums in zip(*L)]print(res)[{'a': [1, 4, 7]}, {'b': [2, 5, 8]}, {'c': [3, 6, 9]}]
-
蝴蝶不菲
a=[['a','b','c'],[1,2,3],[4,5,6],[7,8,9]] dictionary_values=[dict([(a[0][i],list(zip(*a[1:])[i])) for i in range (len(a)-1)])]输出:[{'a': [1, 4, 7], 'b': [2, 5, 8], 'c': [3, 6, 9]}]