# dict的构造函数使用
di6 =dict()print(di6)#{}#di7= dict(1,2,3) dict expected at most 1 arguments, got 3#print(di7) #TypeError: 'int' object is not iterable# dict 如果只有一个参数,那么就必须是可迭代的 并且里面是要能够转换成对应key alue的#di7 = dict('123=12') ValueError: dictionary update sequence element #0 has length 1; 2 is required 没有构成键值对的意思#print(di7)
di7 =dict(one=1,two=3)print(di7)#{'one':1,'two':3}
di8 =dict({
'1':'1','2':'2'})print(di8)#{'1': '1', '2': '2'}
di9 =dict(zip([1,2,3],[11,22,33]))# zip什么意思?print(di9)
di10 =dict((('one',1),('two',2)))print(di10)
di10 =dict((['one',1),('two',2)])# dict
原文
A mapping object maps hashable values to arbitrary objects. Mappings are mutable objects. There is currently only one standard mapping type, the dictionary.(For other containers see the built-inlist,set,andtuple classes,and the collections module.)#映射对象将可刷新的值映射到任意对象。映射是可变的对象。目前只有一种标准的映射类型,即dictionary。(对于其他容器,请参阅内置的列表、集合和元组类,以及集合模块。)
A dictionary’s keys are almost arbitrary values. Values that are not hashable, that is, values containing lists, dictionaries or other mutable types (that are compared by value rather than by object identity) may not be used as keys. Numeric types used for keys obey the normal rules for numeric comparison:if two numbers compare equal (such as1and1.0) then they can be used interchangeably to index the same dictionary entry.(Note however, that since computers store floating-point numbers as approximations it is usually unwise to use them as dictionary keys.)#dict的key通常可以是任意的值。但是只能是不可变数据。可变数据是不能作为key来使用的。#用于键的数字类型遵循数字的常规规则#如果两个数字比较相等(比如1和1.0),那么它们可以互换使用,为相同的字典条目建立索引。 那么就是相等 就会认为一个键,后面定义的则为最后的标准,也就是别重复相同的key#(不过请注意,由于计算机将浮点数存储为近似值,因此将它们用作字典键通常是不明智的。) 浮点数作为key 不是很明智的 因为会将浮点数存储为近似值 那么如何看是否是近似值呢?
Dictionaries can be created by placing a comma-separated list of key: value pairs within braces,for example:{
'jack':4098,'sjoerd':4127}or{
4098:'jack',4127:'sjoerd'},or by the dict constructor.#字典可以通过在大括号中放置一个以逗号分隔的key: value对列表来创建,例如:{'jack': 4098, 'sjoerd': 4127}或{4098:'jack', 4127: 'sjoerd'},或者通过dict构造函数创建。# dict的构建形式 dict() 能够构建, 也可以直接定义classdict(**kwarg)classdict(mapping,**kwarg)classdict(iterable,**kwarg)
Return a new dictionary initialized from an optional positional argument and a possibly empty set of keyword arguments.# 这里定义的是构造函数 dict 返回一个由可选的位置参数和可能空的关键字参数集初始化的新字典。