Python 官方文档:入门教程 => 点击学习
python创建字典的方法:1.通过dict关键字创建;2.通过二元组列表创建;3.通过字典推导式创建;4.通过dict.fromkeys()函数创建;在Python中创建字典的方法有以下几种通过dict关键字创建>>>
python创建字典的方法:1.通过dict关键字创建;2.通过二元组列表创建;3.通过字典推导式创建;4.通过dict.fromkeys()函数创建;
在Python中创建字典的方法有以下几种
通过dict关键字创建
>>> dic = dict(spam = 1, egg = 2, bar =3)
>>> dic
{'bar': 3, 'egg': 2, 'spam': 1}
通过二元组列表创建
>>> list = [('spam', 1), ('egg', 2), ('bar', 3)]
>>> dic = dict(list)
>>> dic
{'bar': 3, 'egg': 2, 'spam': 1}
通过字典推导式创建
>>> dic = {i:2*i for i in range(3)}
>>> dic
{0: 0, 1: 2, 2: 4}
通过dict.fromkeys()函数创建
>>> dic = dict.fromkeys(range(3), 'x')
>>> dic
{0: 'x', 1: 'x', 2: 'x'}
--结束END--
本文标题: python中可以使用哪些方法创建字典
本文链接: https://www.lsjlt.com/news/112447.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0