如何用dict()函数创建一个字典,代码是什么
Admin 2022-08-24 群英技术资讯 499 次浏览
dict() 函数用于创建一个字典。
class dict(**kwarg)
class dict(mapping, **kwarg)
class dict(iterable, **kwarg)
**kwargs -- 关键字
mapping -- 元素的容器。
iterable -- 可迭代对象。
返回一个字典。
# 创建空字典方法1 d = dict() print(type(d)) # <class 'dict'> # 创建空字典方法2 d2 = {} print(type(d2)) # <class 'dict'> # 创建一个标准的字典 dict2 = {'name':'bayi','age':33} print(type(dict2)) # <class 'dict'> print(dict2) # {'name': 'bayi', 'age': 33} # 创建一个传入 关键字=值 的字典 dict3 = dict(name='bayi', age=33, sex='男') print(type(dict3)) # <class 'dict'> print(dict3) # {'name': 'bayi', 'age': 33, 'sex': '男'} # 映射函数方式来构造字典 dict4 =dict(zip(['name','age','address'],['bayi',33,'广东'])) print(type(dict4)) # <class 'dict'> print(dict4) # {'name': 'bayi', 'age': 33, 'address': '广东'} # 可迭代对象(能用for循环的对象)方式来构造字典 # Python中的可迭代对象有:列表、元组、字典、字符串; # dict5 = dict([['one',1],['two',2],['three',3]]) # 方式1 # dict5 = dict([('one',1),('two',2),('three',3)]) # 方式2 # dict5 = dict((('one',1),('two',2),('three',3))) # 方式3 dict5 = dict((['one',1],['two',2],['three',3])) # 方式4 print(type(dict5)) # <class 'dict'> print(dict5) # {'one': 1, 'two': 2, 'three': 3}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
提到数据可视化,我们会想到Plotly、Matplotlib、Pyecharts等可视化库,或者一些商用软件Tableau、FineBI等等。如果你希望操作更简单、展现效果更强大,那么这款工具bi
内容介绍sort()方法是什么如何妙用sorted()方法总结在Python中,你可以使用sorted()方法或sort()方法对数据进行排序。在本文中,我将提供sorted()和
Python基础学习之字符串操作 字符串是编程中最常遇到的类型,所以掌握好字符串的常用操作方法,非常的必要! 1. 字符串的切片 [字符串的切片是最常见的字符串操作,必须要掌握;
python正则表达式re.search()的基本使用教程是什么?re.search会匹配整个字符串,并返回第一个成功的匹配。如果匹配失败,则返回None
PyQt提供了一个设计良好的窗口控件集合,具有更方便的操作性。学过VB的同学会知道,相比与VB的使用,在界面设计上元素更丰富,这篇文章主要介绍了基于PyQt5完成的图转文功能,需要的朋友可以参考下
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008