# 字典魔法:
# @staticmethod 静态方法:
# 1、字典格式
# dic = {
# # "k1":"v1" --键值对 (key:values)
# # "k2":"v2"
# # "k3":"v3"
# }
# 2、fromkeys ---根据序列(键值)创建字典,指定统一的值
# v = dict.fromkeys(["asdsad",1321,"jkllj","我是好人"])
# # print(v)
# 输出结果:{'asdsad': None, 1321: None, 'jkllj': None, '我是好人': None}
# v = dict.fromkeys(["asdsad",1321,"jkllj","我是好人"],"那是必须的")
# print(v)
# 输出结果:{'asdsad': '那是必须的', 1321: '那是必须的', 'jkllj': '那是必须的', '我是好人': '那是必须的'}
# 3、查找对应键值的值
# dic = {
# "k1":"v1","sad":"qeqwe","k2":"456"
# }
# v = dic["k1111"]
# print(v)
# 输出结果:报错!!!!keyerror
# v1= dic.get("k1",11111)
# print(v1,dic)
# 输出结果:v1 {'k1': 'v1', 'sad': 'qeqwe', 'k2': '456'}
# 4、删除并获取值
# dic = {
# "k1":"v1","sad":"qeqwe","k2":"456"
# }
# v1 = dic.pop("k1")
# v2 = dic.popitem()
# print(dic,v1,v2)
# 输出结果:{'sad': 'qeqwe'} v1 ('k2', '456')
# 5、设置值 --可用于增加键值对
# 已存在,不设置,获取当前key对应的values值
# 不存在,设置,获取当前key对应的values值
# dic = {
# "k1":"v1","sad":"qeqwe","k2":"456"
# }
# v = dic.setdefault("k111","123")
# print(dic,v)
# 输出结果;{'k1': 'v1', 'sad': 'qeqwe', 'k2': '456', 'k111': '123'} 123
# 6、update ---更新 修改键值对
# dic = {
# "k1":"v1","sad":"qeqwe","k2":"456"
# }
# dic.update({"k1":"v1","www":"eeeee"})
# print(dic)
# 输出结果:{'k1': 'v1', 'sad': 'qeqwe', 'k2': '456', 'www': 'eeeee'}
# dic.update({"k1":123,"sad":"qqqqqqq"})
# print(dic)
# 输出结果:{'k1': 123, 'sad': 'qqqqqqq', 'k2': '456'}
# 7、keys values 键值对的查找
# dic = {
# "k1":"v1","sad":"qeqwe","k2":"456"
# }
# v = dic.keys()
# v1 = dic.values()
# v2 = dic.items()
# print(v,v1,v2)
# 输出结果:dict_keys(['k1', 'sad', 'k2']) dict_values(['v1', 'qeqwe', '456'])dict_items([('k1', 'v1'), ('sad', 'qeqwe'), ('k2', '456')])
##########黑暗魔法#############
# 1、字典的value可以是任何值
# info = {
# "k1": 18,
# "k2": True,
# "k3": [
# 11,
# [],
# (),
# 22,
# 33,
# {
# 'kk1': 'vv1',
# 'kk2': 'vv2',
# 'kk3': (11,22),
# }
# ],
# "k4": (11,22,33,44)
# }
# print(info)
# 2、布尔值(1,0)、列表、字典不能作为字典的key
# info ={
# 1: 'asdf',
# "k1": 'asdf',
# True: "123",
# # [11,22]: 123
# (11,22): 123,
# # {'k1':'v1'}: 123
#
# }
# print(info)
# 输出结果:{1: '123', 'k1': 'asdf', (11, 22): 123}
# 3、字典无序
# info = {
# # "k1": 18,
# # "k2": True,
# # "k3": [
# # 11,
# # [],
# # (),
# # 22,
# # 33,
# # {
# # 'kk1': 'vv1',
# # 'kk2': 'vv2',
# # 'kk3': (11,22),
# # }
# # ],
# # "k4": (11,22,33,44)
# # }
# # print(info)
# 4、索引方式找到指定元素,key索引
# info = {
# "k1": 18,
# 2: True,
# "k3": [
# 11,
# [],
# (),
# 22,
# 33,
# {
# 'kk1': 'vv1',
# 'kk2': 'vv2',
# 'kk3': (11,22),
# }
# ],
# "k4": (11,22,33,44)
# }
# v = info['k1']
# print(v)
# v = info[2]
# print(v)
# v = info['k3'][5]['kk3'][0]
# print(v)
# 输出结果:11
# 5、字典支持 del 删除,可删除指定键值对
# info = {
# "k1": 18,
# "k3": [
# 11,
# [],
# (),
# 22,
# 33,
# {
# 'kk1': 'vv1',
# 'kk2': 'vv2',
# 'kk3': (11,22),
# }
# ],
# "k4": (11,22,33,44)
# }
# del info['k1']
#
# del info['k3'][5]['kk1']
# print(info)
# 输出结果:{'k3': [11, [], (), 22, 33, {'kk2': 'vv2', 'kk3': (11, 22)}], 'k4':(11, 22, 33, 44)}
# 6、for 循环迭代:
# info = {
# "k1": 18,
# 2: True,
# "k3": [
# 11,
# [],
# (),
# 22,
# 33,
# {
# 'kk1': 'vv1',
# 'kk2': 'vv2',
# 'kk3': (11,22),
# }
# ],
# "k4": (11,22,33,44)
# }
# for item in info:
# print(item)
# 输出结果: k1
# 2
# k3
# k4
# for items in info.keys():
# print(items)
# 输出结果:k1
# 2
# k3
# k4
# for i in info.values():
# print(i)
# 输出结果: 18
# True
# [11, [], (), 22, 33, {'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (11, 22)}]
# (11, 22, 33, 44)
# for ii in info.keys():
# print(ii,info[ii])
# 输出结果:2 True
# k3 [11, [], (), 22, 33, {'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (11, 22)}]
# k4 (11, 22, 33, 44)
# for k,v in info.items():
# print(k,v)
# 输出结果:2 True
# k3 [11, [], (), 22, 33, {'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (11, 22)}]
# k4 (11, 22, 33, 44)
# 7、布尔值:True 1 , False 0
# dic = {
# "k1":'asdf',
# True:"123",
# [11,22]: 123,
# (11,22): 123,
# {'k1':' v1'}: 123
# }
# print(dic)
python基础语法---字典魔法
最新推荐文章于 2024-09-04 10:31:20 发布