python 字典(dict)增删改查及方法

                                            强大自己是唯一获得幸福的途径,这是长远的,而非当下的玩乐!


直接赋值

>>> dic1 = {"name": "zuowei", "age": 18, "hobby": "lp"}
>>> dic1["sex"] = "男"
>>> print(dic1)
{'name': 'zuowei', 'age': 18, 'hobby': 'lp', 'sex': '男'}
>>> dic1["age"] = 8           # 有则改,无则加
>>> print(dic1)
{'name': 'zuowei', 'age': 8, 'hobby': 'lp', 'sex': '男'}

setdefult(self, k, d=None)

>>> dic1 = {"name": "zuowei", "age": 18, "hobby": "lp"}
>>> dic1.setdefault("sex", "男")
>>> print(dic1)
{'name': 'zuowei', 'age': 18, 'hobby': 'lp', 'sex': '男'}
>>> dic1.setdefault("age", 8)      # 有则不变,无则添加
18                                 # 如果有这个key,会返回已存在的值,不会改变
>>> print(dic1)
{'name': 'zuowei', 'age': 18, 'hobby': 'lp', 'sex': '男'}

pop(self, k, d=None)  根据key删除

>>> dic1 = {"name": "zuowei", "age": 18, "hobby": "lp"}
>>> dic1.pop("hobby")
'lp'                      # 会有返回值
>>> print(dic1)
{'name': 'zuowei', 'age': 18}

clear(清空)

>>> dic1 = {"name": "zuowei", "age": 18, "hobby": "lp"}
>>> dic1.clear()
>>> print(dic1)
{}

del (根据key删除)

>>> dic1 = {"name": "zuowei", "age": 18, "hobby": "lp"}
>>> del dic1["hobby"]
>>> print(dic1)
{'name': 'zuowei', 'age': 18}

直接赋值(拷贝的增加。。)

>>> dic1 = {"name": "zuowei", "age": 18, "hobby": "lp"}
>>> dic1["sex"] = "男"
>>> print(dic1)
{'name': 'zuowei', 'age': 18, 'hobby': 'lp', 'sex': '男'}
>>> dic1["age"] = 8           # 有则改,无则加
>>> print(dic1)
{'name': 'zuowei', 'age': 8, 'hobby': 'lp', 'sex': '男'}

update(self, E=None, **F)

>>> dic1 = {"name": "zuowei", "age": 18, "hobby": "lp"}
>>> dic2 = {"name": "lp", "age": 18, "height": 150}
>>> dic2.update(dic1)
>>> print(dic2)                     # 覆盖更新,有则覆盖,无则添加
{'name': 'zuowei', 'age': 18, 'height': 150, 'hobby': 'lp'}

根据key查询

>>> dic1 = {"name": "zuowei", "age": 18, "hobby": "lp"}
>>> print(dic1["name"])
zuowei
>>> print(dic1["hobby"])
lp

get(self, k, d=None)

>>> dic1 = {"name": "zuowei", "age": 18, "hobby": "lp"}
>>> print(dic1["name"])
zuowei
>>> print(dic1["hobby"])
lp
>>> dic1.get("name")
'zuowei'
>>> dic1.get("age")
18

for循环

>>> dic1 = {"name": "zuowei", "age": 18, "hobby": "lp"}
>>> for k, v in dic1.items():
...     print(k, v)
...
name zuowei
age 18
hobby lp

其他方法

clear(清空)

                 上面已说明

copy

                  请参见深浅copy --> https://blog.csdn.net/z_bright/article/details/84648996

fromkeys(*args **kwargs)

>>> l1 = ["name",  "age", "hobby"]
>>> dic1 = dict.fromkeys(l1, None)
>>> print(dic1)
{'name': None, 'age': None, 'hobby': None}

get

           上面已说明

items(返回一个列表,内为key+value组成的元祖,所以上面for循环遍历 字典.items,可以对应两个值并打印)

>>> dic1 = {"name": "zuowei", "age": 18, "hobby": "lp"}
>>> print(dic1.items())
dict_items([('name', 'zuowei'), ('age', 18), ('hobby', 'lp')])

keys(返回字典的key)

>>> dic1 = {"name": "zuowei", "age": 18, "hobby": "lp"}
>>> print(dic1.keys())
dict_keys(['name', 'age', 'hobby'])

values(返回字典的value)

>>> dic1 = {"name": "zuowei", "age": 18, "hobby": "lp"}
>>> print(dic1.values())
dict_values(['zuowei', 18, 'lp'])

 

pop

          上面已说明

popitem(随机返回并删除字典中的一对键和值(一般删除末尾对)。字典为空则报错)

>>> dic1 = {"name": "zuowei", "age": 18, "hobby": "lp"}
>>> dic1.popitem()
('hobby', 'lp')                    # 返回值,可以赋值给其他变量做其他处理
>>> print(dic1)
{'name': 'zuowei', 'age': 18}

setdefault 

              上面已说明

update

                          上面已说明

你可能感兴趣的:(python 字典(dict)增删改查及方法)