来自python的【dict字典总结】缺iter

本文详细介绍了Python字典的定义、构造、读取、修改、更新及删除等操作,包括dict()构造函数、get()、setdefault()、update()、pop()等方法。还探讨了字典内置函数如len()、str()和type(),以及如何通过iter()遍历字典。文章强调了字典中key的唯一性和value的可变性,以及键值对的添加、修改和删除策略。
摘要由CSDN通过智能技术生成

字典(映射)定义

  • dict字典,也称作映射,目前python只有一种标准的映射类型。
  • dict是可变数据类型,内部可以存储任意类型对象。
  • 定义形式常见的:dict{key1:value,key2:value},值得注意的是,key必须是唯一的,当有相同的key出现,则以最后一个数据为准,但是value可以不是唯一的。并且key必须是不可变数据,如字符串、数字、元组等,不然会报错哦TypeError: unhashable type
  • 能够嵌套
di1 = {
   '1':'one','w':'two'}
print(di1) #{'1': 'one', 'w': 'two'}
di2 = {
   '1':'1',"1":"2"}
print(di2) #{'1': '2'} 以最后一个为准
#di3 = {[1]:1}
#print(di3) #TypeError: unhashable type: 'list' 要是不可变的
  • 如果key 相等,则会以最后一个数据为准,如果key的值能够通过 == 判定是相等,则会视为同一个key,并且是以转换后的形式显示
# key1 key1.0
di3 = {
   1:1,1.0:7,True:9}
print(di3)#{1:9} # true =1 有点意思
  • 浮点数作为key时,这是不明智的选择,因为计算机会将浮点数转换为近似值,因此就会丢掉精度。value的浮点数也会选择精度显示,看电脑的设置。
# 浮点数精度
# 浮点数精度
di4 = {
   1.0000000000000000000123 :12}
print(di4)#{1.0: 12}
di5 = {
   12:1.0000000000000000000000023}
print(di5)

dict()构造函数

  • dict () 当没有参数的时候,返回一个空字典{}
  • dict(keyword) 只能有一个参数,并且这个参数是要可迭代的,并且能够成对出现,构成键值对的。
  • 只能有一个参数,并且在一个参数内构成成对存在的数据,zip可以对应
# 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-in list, set, and tuple 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 as 1 and 1.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() 能够构建, 也可以直接定义
class dict(**kwarg)
class dict(mapping, **kwarg)
class dict(iterable, **kwarg)
Return a new dictionary initialized from an optional positional argument and a possibly empty set of keyword arguments.
# 这里定义的是构造函数 dict 返回一个由可选的位置参数和可能空的关键字参数集初始化的新字典。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值