列表列表中的Dict Comprehension python [英] Dict Comprehension python from list of lists

查看:60
本文介绍了列表列表中的Dict Comprehension python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表列表,我正在尝试从列表中制作字典.我知道如何使用这种方法.用Python中的列表列表创建字典

I have a list of lists and I am trying to make a dictionary from the lists. I know how to do it using this method. Creating a dictionary with list of lists in Python

我想做的是使用第一个列表中的元素作为键来构建列表,而具有相同索引的其余项目将是值列表.但是我不知道从哪里开始.每个列表的长度相同,但是列表的长度不同

What I am trying to do is build the list using the elements in the first list as the keys and the rest of the items with the same index would be the list of values. But I can't figure out where to start. Each list is the same length but the length of the lists vary

exampleList = [['first','second','third'],['A','B','C'], ['1','2','3']]

resultDict = {'first':['A','1'],'second':['B','2'],'third':['C','3']}

推荐答案

请注意 exampleList 可以是任意长度的情况.

Take care of the case when exampleList could be of any length..

exampleList = [['first','second','third'],['A','B','C'], ['1','2','3'],[4,5,6]]

z=list(zip(*exampleList[1:]))
d={k:list(z[i])  for i,k in enumerate(exampleList[0])}
print(d)

输出

{'first': ['A', '1', 4], 'second': ['B', '2', 5], 'third': ['C', '3', 6]}

这篇关于列表列表中的Dict Comprehension python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆