gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:35:32 24 4
gpt4 key购买 nike

我有一个列表列表,我正在尝试从列表中制作字典。我知道如何使用这种方法来做到这一点。 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com