python学习笔记——字典 创建和访问字典,增添和修改,删除字典,字典的拷贝

python中唯一的映射类型:字典
序列类型:列表,元组,字符串
字典的使用是很简单的,标志性符号是大括号{ },关键字为key-value,键值组合称为项,key可以是整数,变量,字符串类型,浮点型等。


一.字典:当索引不好用时

1.创建和访问字典

传统的创建:

>>> dict1={"李宁":"一切皆有可能","耐克":"Just do it","阿迪达斯":"Impossible is nothing"}
#访问字典
>>> print("耐克的口号是:",dict1["耐克"])
耐克的口号是: Just do it

元组或列表创建:
(利用工厂函数dict)

>>> dict3 = dict((("F",70),("i",105),("s",115),("h",104)))
>>> dict3
{'F': 70, 'i': 105, 's': 115, 'h': 104}
#访问字典
>>> dict3["F"]
70

----------


>>> dict3 = dict((["F",70],["i",105],["s",115],["h",104]))
>>> dict3
{'F': 70, 'i': 105, 's': 115, 'h': 104}

关键字参数创建:
(利用工厂函数dict)

>>> dict4=dict(李宁="一切皆有可能",耐克="Just do it")
>>> dict4
{'李宁': '一切皆有可能', '耐克': 'Just do it'}

#访问字典
>>> dict4["李宁"]
'一切皆有可能'

#修改
>>>dict4["李宁"]=“李宁的口号是让一切皆有可能”
{'李宁': '李宁的口号是让一切皆有可能', '耐克': 'Just do it'}
>>> dict4
{'李宁': '李宁的口号是让一切皆有可能', '耐克': 'Just do it'}

#添加
>>>dict4['阿迪达斯']= 'Impossible is nothing'
>>> dict4
{'李宁': '李宁的口号是让一切皆有可能', '耐克': 'Just do it', '阿迪达斯': 'Impossible is nothing'}


使用字典内建函数fromkeys创建:

使用help得到:
Help on built-in function fromkeys:

fromkeys(iterable, value=None, /) method of builtins.type instance
Create a new dictionary with keys from iterable and values set to value.

>>> dict1={}
>>> dict1.fromkeys((1,2,3))
{1: None, 2: None, 3: None}
>>> dict1.fromkeys((1,2,3),"Number")
{1: 'Number', 2: 'Number', 3: 'Number'}
>>> dict1.fromkeys((1,2,3),("one","two","three")) 
#这里直接把("one","two","three")当作一个值赋给三个键了,
#并不是我个人以为的{1: 'one', 2: 'two', 3: 'three'}这样结果
{1: ('one', 'two', 'three'), 2: ('one', 'two', 'three'), 3: ('one', 'two', 'three')}
>>> 

2.增添和修改

传统方式:

>>> dict3 = dict((["F",70],["i",105],["s",115],["h",104]))
>>> dict3
{'F': 70, 'i': 105, 's': 115, 'h': 104}
#修改
>>> dict3["i"]=89
>>> dict3
{'F': 70, 'i': 89, 's': 115, 'h': 104}
#增加
>>> dict3["c"]=55
>>> dict3
{'F': 70, 'i': 89, 's': 115, 'h': 104, 'c': 55}
>>> 

setdefault( )方法:

>>>a
 {1:"one"}
>>> a.setdefault(5,"five")
'five'
>>> a
{1: 'one', 5: 'five'}
>>> a.setdefault(6)
>>> a
{1: 'one', 5: 'five', 6: None}

3.访问字典
字典的三个内置方法
keys():
返回字典键的引用

>>> dict1=dict.fromkeys(range(10),"臭袜子")
>>> dict1
{0: '臭袜子', 1: '臭袜子', 2: '臭袜子', 3: '臭袜子', 4: '臭袜子', 5: '臭袜子', 6: '臭袜子', 7: '臭袜子', 8: '臭袜子', 9: '臭袜子'}
>>> for eachKey in dict1.keys():
	print(eachKey)

	
0
1
2
3
4
5
6
7
8
9

values():
返回字典值的引用

>>> dict1=dict.fromkeys(range(10),"臭袜子")
>>> dict1
{0: '臭袜子', 1: '臭袜子', 2: '臭袜子', 3: '臭袜子', 4: '臭袜子', 5: '臭袜子', 6: '臭袜子', 7: '臭袜子', 8: '臭袜子', 9: '臭袜子'}
>>> for eachValue in dict1.values():
	print(eachValue)

	
臭袜子
臭袜子
臭袜子
臭袜子
臭袜子
臭袜子
臭袜子
臭袜子
臭袜子
臭袜子

items():
以元组形式返回字典键值的引用

>>> dict1=dict.fromkeys(range(10),"臭袜子")
>>> dict1
{0: '臭袜子', 1: '臭袜子', 2: '臭袜子', 3: '臭袜子', 4: '臭袜子', 5: '臭袜子', 6: '臭袜子', 7: '臭袜子', 8: '臭袜子', 9: '臭袜子'}

>>> for eachItem in dict1.items():
	print(eachItem)

	
(0, '臭袜子')
(1, '臭袜子')
(2, '臭袜子')
(3, '臭袜子')
(4, '臭袜子')
(5, '臭袜子')
(6, '臭袜子')
(7, '臭袜子')
(8, '臭袜子')
(9, '臭袜子')
>>> 

$$当访问一个不存在的键时,如下,会报错的啊!!
这时推荐使用内建方法get(),也可以使用成员操作符in,not in

>>> dict1=dict.fromkeys(range(5),"haha")
>>> dict1
{0: 'haha', 1: 'haha', 2: 'haha', 3: 'haha', 4: 'haha'}
>>> print(dict1[5])
Traceback (most recent call last):
  File "<pyshell#33>", line 1, in <module>
    print(dict1[5])
KeyError: 5

#使用内建方法get()
>>> print(dict1.get(5))
None
>>> print(dict1.get(5,"没有的哦"))
没有的哦

4.删除字典

清空整个字典,使用clear( )
删除字典中的某一项,用popitem()或者pop(),示例如下:

#popitem()此方法删除项是随机的
>>> a.popitem()
(3, 'three')
#pop()可以指定删除
>>> a.pop(2)
'two'

5.字典的拷贝
使用copy()方法,示例如下:

>>> a= {1:"one",2:"two",3:"three"}
#拷贝与赋值是不同的,看id
>>> b=a.copy()
>>> c=a
>>> c
{1: 'one', 2: 'two', 3: 'three'}
>>> a
{1: 'one', 2: 'two', 3: 'three'}
>>> b
{1: 'one', 2: 'two', 3: 'three'}
>>> id(a)
55905760
>>> id(b)
55606800
>>> id(c)
55905760 #与a的id相同

注:上文仅提到部分内建函数

所有方法:
[‘class’, ‘contains’, ‘delattr’, ‘delitem’, ‘dir’, ‘doc’, ‘eq’, ‘format’, ‘ge’, ‘getattribute’, ‘getitem’, ‘gt’, ‘hash’, ‘init’, ‘init_subclass’, ‘iter’, ‘le’, ‘len’, ‘lt’, ‘ne’, ‘new’, ‘reduce’, ‘reduce_ex’, ‘repr’, ‘setattr’, ‘setitem’, ‘sizeof’, ‘str’, ‘subclasshook’, ‘clear’, ‘copy’, ‘fromkeys’, ‘get’, ‘items’, ‘keys’, ‘pop’, ‘popitem’, ‘setdefault’, ‘update’, ‘values’]


-,blog会持续更新_

发布了43 篇原创文章 · 获赞 46 · 访问量 4530

猜你喜欢

转载自blog.csdn.net/S_123789/article/details/81637851