随笔 - 143,  文章 - 28,  评论 - 6,  阅读 - 17万

 

复制代码
# 字典
# dict
# dic = {
#     "k1": 'v1',
#     "k2": 'v2'
# }
# 1 根据序列,创建字典,并指定统一的值
# v = dict.fromkeys(["k1",123,"999"],123)
# print(v)

# 2 根据Key获取值,key不存在时,可以指定默认值(None)
# v = dic['k11111']
# print(v)
# v = dic.get('k1',111111)
# print(v)

# 3 删除并获取值
# dic = {
#     "k1": 'v1',
#     "k2": 'v2'
# }
# v = dic.pop('k1',90)
# print(dic,v)
# k,v = dic.popitem()
# print(dic,k,v)

# 4 设置值,
# 已存在,不设置,获取当前key对应的值
# 不存在,设置,获取当前key对应的值
# dic = {
#     "k1": 'v1',
#     "k2": 'v2'
# }
# v = dic.setdefault('k1111','123')
# print(dic,v)

# 5 更新
# dic = {
#     "k1": 'v1',
#     "k2": 'v2'
# }
# dic.update({'k1': '111111','k3': 123})
# print(dic)
# dic.update(k1=123,k3=345,k5="asdf")
# print(dic)

# 6 keys()  7 values()   8 items()   get   update
##################################################
# 1.基本机构
# info = {
#     "k1": "v1", # 键值对
#     "k2": "v2"
# }
# 2.字典的value可以是任何值
# info = {
#     "k1": 18,
#     "k2": True,
#     "k3": [
#         11,
#         [],
#         (),
#         22,
#         33,
#         {
#             'kk1': 'vv1',
#             'kk2': 'vv2',
#             'kk3': (11,22),
#         }
#     ],
#     "k4": (11,22,33,44)
# }
# print(info)

# 3.布尔值(1,0)、列表、字典不能作为字典的key
# info ={
#     1: 'asdf',
#     "k1": 'asdf',
#     True: "123",
#     # [11,22]: 123
#     (11,22): 123,
#     # {'k1':'v1'}: 123
#
# }
# print(info)

# 4.字典无序
# info = {
#     "k1": 18,
#     "k2": True,
#     "k3": [
#         11,
#         [],
#         (),
#         22,
#         33,
#         {
#             'kk1': 'vv1',
#             'kk2': 'vv2',
#             'kk3': (11,22),
#         }
#     ],
#     "k4": (11,22,33,44)
# }
# print(info)

# 5.索引方式找到指定元素
# 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)

# 6.字典支持 del 删除
# info = {
#     "k1": 18,
#     2: True,
#     "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)

# 7.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)
#
# for item in info.keys():
#     print(item)

# for item in info.values():
#     print(item)

# for item in info.keys():
#     print(item,info[item])

# for k,v in info.items():
#     print(k,v)

# True 1  False 0
# info ={
#     "k1": 'asdf',
#     True: "123",
#     # [11,22]: 123
#     (11,22): 123,
#     # {'k1':' v1'}: 123
#
# }
# print(info)
复制代码

 

posted on   月零Ray  阅读(290)  评论(0编辑  收藏  举报
编辑推荐:
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
阅读排行:
· Sdcb Chats 技术博客:数据库 ID 选型的曲折之路 - 从 Guid 到自增 ID,再到
· 语音处理 开源项目 EchoSharp
· 《HelloGitHub》第 106 期
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 使用 Dify + LLM 构建精确任务处理应用

< 2025年1月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示