加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 字典的子列表

发布时间:2020-12-20 11:26:32 所属栏目:Python 来源:网络整理
导读:所以我有: 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"]] dic
所以我有:

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 == e
True

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读