python - 想要将列表的元素更改为Python中的字典键

标签 python python-3.x list dictionary

我想将列表更改为字典,并且还想将列表的第一个元素作为字典的键,其他元素将是字典中该键的结果。提前致谢。

这就是我所拥有的:

lst = ['a', 'b', 'c', 'd']
print (list)
['a', 'b', 'c', 'd']

这就是我想要的:

dic = {'a':['b', 'c', 'd']}
print(dic)
{'a': ['b', 'c', 'd']}

print (dic['a'])
['b', 'c', 'd']

最佳答案

你可以尝试:

lst = ['a', 'b', 'c', 'd']
dct = {lst[0]:lst[1:]}

它会给你想要的结果

关于python - 想要将列表的元素更改为Python中的字典键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62459349/

相关文章:

python - 在python中读取arduino串行连接

python - 如何在 python 中找到函数对象的必需参数?

python - 在Python中用数据帧中的平均值替换 '?'时出错

python - 有没有一种简单的方法可以按值删除列表元素?

python - 一个列表中的两个总和

python - 在 Python 中,有效地确定两个列表是否是彼此的移位副本

python - 将所有构造函数参数作为实例属性添加到 PyCharm 中的类

python - 如果包含非零元素则获取列表

python - 使用 LDAP 的 Flask 身份验证

python-3.x - python中需要正则表达式帮助