本文共 3217 字,大约阅读时间需要 10 分钟。
字典dictionary
字段是python中唯一的映射关系。采用键值对(key-value)存储数据。Python对key进行哈希函数运算,根据计算结果决定value的存储位置,所以字典是无序列的,且key必须是可哈希的。可哈希表示key必须是不可变类型,如数字,字符串,元组。# -*-coding:utf-8 -*-__date__ = '2018/2/7 9:50'__author__ = 'xiaojiaxin'__file_name__ = 'dict1'#创建字典方法1dict1={1:[1,2,3],2:"1000","name":"jiaxin"}print(dict1)print(dict1[1])print(dict1["name"])a=list((1,2,3)) #创建列表的新方法print(a)a=list([1,2,3]) #只需要在list()放一个序列,就可以转换成一个列表print(a)a=list("123")print(a)#创建字典方法2,不常用dict2=dict(((1,"a"),(2,'b'),(3,'c'),)) #里面放键值对print(dict2)#增删改查#增加dict2["age"]=50print(dict2)#键存在不改值,不存在就创建键和值return1=dict2.setdefault("home","ZHEJIANG") #setdefault有返回值print(dict2)print(return1)dict2.setdefault("hobby")print(dict2)#查print(dict2["hobby"])print(dict2[1])print(dict2.keys()) #查找字段的所有键#dict_keys([1, 2, 3, 'age', 'home', 'hobby']) #不是列表类型,是dict_keys新的数据类型print(list(dict2.keys())) #转换成list类型使用print(dict2.values())print(dict2.items())#改dict2["name"]="jiaxin1"print(dict2)dict3={"name":"jiaxin3",10:"10"}dict2.update(dict3) #原值会被新值覆盖掉print(dict3)print(dict2)#删del dict3["name"]print(dict3)print(dict3.clear())del dict3ruturn2=dict2.pop(10)print(dict2)print(ruturn2)return3=dict2.popitem() #随机删除一组键值对,意义不大print(dict2)print(return3)
{1: [1, 2, 3], 2: '1000', 'name': 'jiaxin'}
[1, 2, 3]jiaxin[1, 2, 3][1, 2, 3]['1', '2', '3']{1: 'a', 2: 'b', 3: 'c'}{1: 'a', 2: 'b', 3: 'c', 'age': 50}{1: 'a', 2: 'b', 3: 'c', 'age': 50, 'home': 'ZHEJIANG'}ZHEJIANG{1: 'a', 2: 'b', 3: 'c', 'age': 50, 'home': 'ZHEJIANG', 'hobby': None}Noneadict_keys([1, 2, 3, 'age', 'home', 'hobby'])[1, 2, 3, 'age', 'home', 'hobby']dict_values(['a', 'b', 'c', 50, 'ZHEJIANG', None])dict_items([(1, 'a'), (2, 'b'), (3, 'c'), ('age', 50), ('home', 'ZHEJIANG'), ('hobby', None)]){1: 'a', 2: 'b', 3: 'c', 'age': 50, 'home': 'ZHEJIANG', 'hobby': None, 'name': 'jiaxin1'}{'name': 'jiaxin3', 10: '10'}{1: 'a', 2: 'b', 3: 'c', 'age': 50, 'home': 'ZHEJIANG', 'hobby': None, 'name': 'jiaxin3', 10: '10'}{10: '10'}None{1: 'a', 2: 'b', 3: 'c', 'age': 50, 'home': 'ZHEJIANG', 'hobby': None, 'name': 'jiaxin3'}10{1: 'a', 2: 'b', 3: 'c', 'age': 50, 'home': 'ZHEJIANG', 'hobby': None, 'name': 'jiaxin3'}其他操作及方法
#其他操作及方法#深浅拷贝学完再看dict4=dict2.fromkeys(["host1","host2","host3"],"test")print(dict4)dict5=dict2.fromkeys(["host1","host2","host3"],["test","test2"])print(dict5)dict5["host2"][1]='test3' #实际效果是全部都改了print(dict5)
{'host1': 'test', 'host2': 'test', 'host3': 'test'}
{'host1': ['test', 'test2'], 'host2': ['test', 'test2'], 'host3': ['test', 'test2']}{'host1': ['test', 'test3'], 'host2': ['test', 'test3'], 'host3': ['test', 'test3']}#排序
dict6={1:"111",2:"222",3:"13"}print(sorted(dict6)) #只对键进行排序,只输出键print(sorted(dict6.items())) #对键进行排序,输出键值对
[1, 2, 3]
[(1, '111'), (2, '222'), (3, '13')]#遍历 很重要!!!!!
print(dict2)for i in dict2: print(i)
# 1# 2# 3# age# home# hobby
for m,n in enumerate(dict2): print(m,"---",n)# 0 --- 1# 1 --- 2# 2 --- 3# 3 --- age# 4 --- home# 5 --- hobbyfor m1,n1 in dict2.items(): #效率不高 print(m1,">>>",n1)# 1 >>> a# 2 >>> b# 3 >>> c# age >>> 50# home >>> ZHEJIANG# hobby >>> Nonefor i in dict2: print(i,dict2[i])# 1 a# 2 b# 3 c# age 50# home ZHEJIANG# hobby None
转载于:https://blog.51cto.com/10777193/2069809