python – 字典的子列表

python – 字典的子列表,第1张

概述所以我有: a = [["Hello", "Bye"], ["Morning", "Night"], ["Cat", "Dog"]] 我想把它转换成字典. 我试过用: i = iter(a) b = dict(zip(a[0::2], a[1::2])) 但它给了我一个错误:TypeError:unhashable type:’list’ 只是: >>> a = [["Hello", "Bye 所以我有:

a = [["Hello","Bye"],["Morning","Night"],["Cat","Dog"]]

我想把它转换成字典.

我试过用:

i = iter(a)  b = dict(zip(a[0::2],a[1::2]))

但它给了我一个错误:TypeError:unhashable type:’List’

解决方法 只是:

>>> a = [["Hello","Dog"]]>>> dict(a){'Cat': 'Dog','Hello': 'Bye','Morning': 'Night'}

我喜欢python的简单性

您可以看到构建字典的所有方法的here:

To illustrate,the following examples all return a dictionary equal to {"one": 1,"two": 2,"three": 3}:

>>> a = dict(one=1,two=2,three=3)>>> b = {'one': 1,'two': 2,'three': 3}>>> c = dict(zip(['one','two','three'],[1,2,3]))>>> d = dict([('two',2),('one',1),('three',3)]) #<-Your case(Key/value pairs)>>> e = dict({'three': 3,'one': 1,'two': 2})>>> a == b == c == d == eTrue
总结

以上是内存溢出为你收集整理的python – 字典的子列表全部内容,希望文章能够帮你解决python – 字典的子列表所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1192792.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存