# -- coding: utf-8 --
# @time : 2022/7/19 21:51
# @file : 10pytest基本数据类型-dic.py
# @software: pycharm
D = {"a": "123",
"b": "456",
"c": 888}
D.copy()
print(id(D))
print(id(D.copy()))
print(D.copy()) # D的浅拷贝
print(D.get("a")) # 获取key为"a"的值 D["a"] if a in D, else d. d defaults to None.
print(D.items()) # dict_items([('a', '123'), ('b', '456'), ('c', 888)])
print(D.keys()) # 返回字典中所有的key dict_keys(['a', 'b', 'c'])
print(D.values()) # 返回字典中所有的值 dict_values(['123', '456', 888])
# print(D.pop("1")) #KeyError: '1'
print(D.pop("a")) # 移除key为"a"并输出key对应的值 123
print(D.popitem()) # 返回并删除字典中的最后一对键和值。('c', 888)
print(D.items())
print(D.setdefault("b")) # 如果key-b在D里边存在,则返回D.get(b),和get类似
print(D.setdefault("c", "999")) # also set D[c]=999 if c not in D
print(D.setdefault("d", "777")) # also set D[d]=777 if d not in D
print(D.items())
print(D.clear()) # 清空字典 返回None. Remove all items from D.
print("============以上方法为python-字典的内置方法==================")
# 除了以上方法外,还有以下常用的方法
D["e"] = 000
D["f"] = "你好呀"
print(D)
print(D["f"]) # 访问字典里边的值
D["f"] = "我不好" # 修改字典
print(D)
del D["f"] # 删除字典元素
print(D)
print(len(D)) # 计算字典的键的总数。
print(type(D)) # 返回输入的变量类型
print(str(D)) # 输出字典可打印的字符串表示。
原创文章,作者:dweifng,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/275489.html