python - 来自列表列表的字典理解python

标签 python dictionary dictionary-comprehension

我有一个列表列表,我正在尝试从列表中制作字典。我知道如何使用这种方法来做到这一点。 Creating a dictionary with list of lists in Python

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

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

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

最佳答案

解压缩并使用 zip 后跟字典理解来获得与第一个元素的映射似乎是可读的。

result_dict = {first: rest for first, *rest in zip(*exampleList)}

关于python - 来自列表列表的字典理解python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46612230/

相关文章:

python - 一个很酷的 python 脚本,让青少年学习 python 对编程感到兴奋?

python - 如何在可变列数匹配的 Pandas 中选择行?

python - 在 python 中搜索字典列表

python - 为什么生成器表达式这么叫?

Python字典逐点输出

python - python 是否有 Matlab 的 `ans` 变量捕获未存储在任何变量中的返回值?

python - 将 anaconda 包导入 IDLE?

multithreading - std::map 在 c++11 中插入线程安全吗?

string - 理解 Scala 中的 String 和 map 方法

python - 映射拼字游戏字母分布的字典理解