字典类型(dict)的使用

字典类型(dict)的使用

返回首页

  在字典中要注意:字典无序,用于存储详细信息。字典是可变数据类型

  字典的键 "key" 是唯一的,并且是不可变的数据类型,但值 "value" 可以是各种的数据类型,比如是字典、列表。 

  字典是Python中,唯一具有映射关系的数据类型:可一一对应,也可一对多。  

  字典的查询速度高于列表,字典的存储空间会很大。

    字典的格式:{ " key " : " value " }   key是键,value是值。通过key获取value。多个key value用 " , " 隔开。

  字典的常用操作有:取值、修改、添加(插入)、查看、循环、更新。

  取值:dict_name [ X ],X是key的值。通过知道key,取value值。 

name_dict={
    1:{"name":"george","age":28},
    2:{"name":"wang","age":20},
    3:{"name":{"name":"tom","age":18}},
4:["rain"], "name":"python", } print(name_dict[1]) print(name_dict[2]["name"]) print(name_dict[3]["name"]["age"]) print(name_dict[4]) print(name_dict["name"])

 

  修改:对字典里的某个值进行数据修改。

name_dict={
    1:{"name":"george","age":28},
    2:{"name":"wang","age":20},
    3:{"name":{"name":"tom","age":18}},
    4:["rain","123455677"],
    "name":"python",
}

print(name_dict)
name_dict[1]["name"] = "George"
name_dict[2]["age"] = 19
name_dict[3]["name"]["name"] = "TOM"
name_dict[3]["name"]["age"] = 180
name_dict[4][0] = "green"
name_dict[4][1] = "180-180-180"
name_dict["name"] = "Python 3.6"
print(name_dict)

 

  添加:对value的值是列表类型的,进行append添加操作。

name_dict={
    1:{"name":"george","age":28},
    2:{"name":"wang","age":20},
    3:{"name":{"name":"tom","age":18}},
    4:["rain","123455677"],
    "name":"python",
}

name_dict[4].append("添加")
print(name_dict)

 

  插入:给字典插入新的key和value数据。

name_dict={
    1:{"name":"george","age":28},
    2:{"name":"wang","age":20},
    3:{"name":{"name":"tom","age":18}},
    4:["rain","123455677"],
    "name":"python",
}
#插入的是key和value
name_dict[5] = "make"
name_dict["BMW"] = "X7"
print(name_dict)

 

  删除:pop()方法,删除指定的key,会返回删除的结果。如果pop删除的key不存在,那就附一个默认的值。

name_dict={
    1:{"name":"george","age":28},
    2:{"name":"wang","age":20},
    3:{"name":{"name":"tom","age":18}},
    4:["rain","123455677"],
    "name":"python",
}

print(name_dict.pop(1))
print(name_dict.pop(10,None))

     popitem()方法:是可以随机删除一个值,因为字典是无序的。不用填写参数,随机删除。

name_dict={
    1:{"name":"george","age":28},
    2:{"name":"wang","age":20},
    3:{"name":{"name":"tom","age":18}},
    4:["rain","123455677"],
    "name":"python",
}

print(name_dict.popitem())

     del方法删除:

name_dict={
    1:{"name":"george","age":28},
    2:{"name":"wang","age":20},
    3:{"name":{"name":"tom","age":18}},
    4:["rain","123455677"],
    "name":"python",
}

del name_dict[2]
del name_dict["name"]
print(name_dict)

 

  查看:查看字典的某个值。 还可以判断要查询的key,是否在字典中。  

name_dict={
    1:{"name":"george","age":28},
    2:{"name":"wang","age":20},
    3:{"name":{"name":"tom","age":18}},
    4:["rain","123455677"],
    "name":"python",
}

print(name_dict)  # 查看字典全部的值
print(name_dict[1])  # 查看字典指定的值,直接输key
print(name_dict[1]["age"])  # 查看字典指定的值,直接输key
print(name_dict["name"])  # 查看字典指定的值,直接输key
print(name_dict.get(10))  # 当查看字典的一个值,如果不存在会报错,为避免报错。用get方法,如值不存在会返回None
print(name_dict.get(2))  # 当查看字典的一个值,如果不存在会报错,为避免报错。用get方法,如值不存在会返回None
print(name_dict.get(2)["name"])  # 当查看字典的一个值,如果不存在会报错,为避免报错。用get方法,如值不存在会返回None

    判断是否在字典中。返回布尔值(bool),存在是True,不存在是False。

name_dict={
    1:{"name":"george","age":28},
    2:{"name":"wang","age":20},
    "name":"python",
}

print(111 in name_dict)  #存在key是True,不在是False

 

  循环:循环字典的方法

print("------- loop -------")
name_dict={
    1:{"name":"george","age":28},
    2:{"name":"wang","age":20},
    3:{"name":{"name":"tom","age":18}},
    4:["rain","123455677"],
    "name":"python",
}

#这样只会打印key
for i in name_dict:
    print(i)

#同时打印key和value (推荐用法)
for i in name_dict:
    print(i,name_dict[i])

#低效率的循环取key and value
for k,v in name_dict.items():
    print(k,v)

#循环key
print(name_dict.keys())

#循环value
print(name_dict.values())

 

  更新:update()方法。相当于合并,但是当update里的参数于前面的字典有相同的key,那么后者覆盖前者。

#######更新 update(),相当于是将两个字段合并
name_dict1={
    1:{"name":"george","age":28},
    3:{"name":{"name":"tom","age":18}},
    "name":"python",
}

name_dict2={
    2:{"name":"wang","age":20},
    4:["rain","123455677"],
    "age":"28",
    "name":"10",
}
#当update()参数里的字典有key和update前的字典key有重合,那么将update的key覆盖前面的。
#name_dict2的name 覆盖name_dict1的name
name_dict1.update(name_dict2)
print(name_dict1)

 

  formkeys( )方法:返回一个新的字典

info = {}
info1=  info.fromkeys([1,2,3],{})
info2=  info.fromkeys([1,2,3],"Test") 
print(info1)  
print(info2)

 

  

------ END ------

posted @ 2020-05-02 18:41  王先生是胖子  阅读(1455)  评论(0编辑  收藏  举报