获取子列表的第一个元素作为python中的字典键 [英] Get first element of sublist as dictionary key in python
本文介绍了获取子列表的第一个元素作为python中的字典键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我看了一下,但没有找到答案(我对python还是很陌生).
I looked but i didn't found the answer (and I'm pretty new to python).
问题很简单.我有一个由子列表组成的列表:
The question is pretty simple. I have a list made of sublists:
ll
[[1,2,3], [4,5,6], [7,8,9]]
我想做的是创建一个字典,将每个子列表的第一个元素作为键,并将对应的子列表的值作为值,例如:
What I'm trying to do is to create a dictionary that has as key the first element of each sublist and as values the values of the coorresponding sublists, like:
d = {1:[2,3], 4:[5,6], 7:[8,9]}
我该怎么做?
推荐答案
使用 dict理解:
{words[0]:words[1:] for words in lst}
输出:
{1: [2, 3], 4: [5, 6], 7: [8, 9]}
这篇关于获取子列表的第一个元素作为python中的字典键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文