Python:字典

'''
1-字典的定义:
    1-dict1 = {}--空字典
    2-type({})-----<class 'dict'>
    3- 字典名 = {键名:值,键名2:值2}
2-字典的优势:
    1-描述清楚
    2-查找方便
    3-扩展性好
3-字典的特性:
    1-它没有下标---不是序列类型!
    2-一种映射--map
    3-字典是mutable 的---可以改变
4-字典的常用操作----dict1 = {'name': 'Jack', 'age': 40}
    1-获取值:dict1['name']
    2-改变值:'name'已经存在,dict1['name'] = 'tom'
    3-新增:这个键名不存在:dict1['weight'] = 160
        1-python 3 ----从尾部增加
        2-python 2 ----无序的
    4-如果需要获取的值对应的键不存在,那么--  KeyError: 'weight'
    5-字典内部元素不存在顺序的概念
    6-字典内部不会存在相同键名
    7-相同键名时候,后面的值会覆盖前面的值---唯一的
    8-键、值的类型:
        1-键的类型:
            1-数值
            2-字符串
            3-元组
            4-列表---*不能作为键--TypeError: unhashable type: 'list'
            5-字典---*不能作为键
            6-键一定是哈希类型--不能改变的!
        2-值的类型:任意类型
    9-键值对一定要成对出现吗?--键值对
    10-  'name' in dict1  bool
5-字典的技巧:
    1-删除字典元素:
        1-del 字典名[键]
        2- pop---有返回值
        3-没有remove
        4- dict1.clear() 与 dict1 = {}  地址不一样
6-字典的遍历:
    1-for one in students:---one 依次取-key 键
    2-students.items()---键值对
        1- [(键1,值1),(键2,值2)]
    3- for name , info in students.items():
7-len()--键值长度
8-d.clear()---只改变该地址的内容
9-d = {}----重新指向
10-得到所有的key返回在类List中  students.keys()  ['jack', 'tom']
11-得到所有的value返回在类List中  students.values()
12-增加元素---d.update({4:'4',5:'5'})

'''''
'''
问题:
    1- 描述不清晰
    2- 如果原本的列表出现元素的个数变化---有可能会影响后续代码
    3- 严格与下标相关
'''

'''
字典的定义要求:
    1- 键:可以的类型:字符串、int 、float、元组、
            不可以是:列表  、字典 ---TypeError: unhashable type: 'list'---不可以改变的类型

    2- 值:任意类型
'''

'''
1- 键名是不是唯一的?---唯一的  后者覆盖前者
2- 字典能不能改变元素的值?--可以的--dict1[键名] = 值---这个键是存在的---修改值
3- 怎么在字典里增加元素?---可以的--dict1[键名] = 值---这个键不存在的---新增键值对
4- 怎么获取字典元素值:--print(dict1['name'])
5- 这个键不存在,获取这个键的值会怎么样?--KeyError: 'age'
6- 我们怎么在字典的指定位置增加元素?--做不到
    1- 没有必要---字典是无序的-----不通过下标获取值
    2- python3语法----增加键值对在--尾部
       python2语法----增加键值对在--随机 
7- 删除键值对:
    1- del dict1['name']
    2- dict1.pop('name')
    3- 没有remove()方法
8- 字典是无序的--没有下标!!!   
'''
# nameList = [#学生列表
#     ['Mike', 25, 180,80],
#     ['Tom', 22, 175,80]
# ]
# # nameList.insert(0,['Mike', 25, 180,80])
# print(nameList[1][1])#tom -- 22

# dict1 = {'name':'tom','age':20}
# # print(type(dict1))#<class 'dict'>
# print(dict1)
'''
特性:
    1- 定义要求:
        1- 键:可以是: int float tuple bool , str---建议使用
             不可以是:list、dict -TypeError: unhashable type: 'list'(不可以改变的类型)
        2- 值:任意类型
        3- 键值对--成对出现

'''
# dict1 = {'name':'tom','age':20}
# 字典里面--键是唯一的

# 1- 获取元素值----如果键不存在,获取这个键对应的值---会报错
# print(dict1['name'])
# print(dict1['weight'])#键不存在----KeyError: 'weight'
# 2- 获取元素个数
# print(len(dict1))
# 3- 修改字典的值---键是存在的
# dict1['name'] = 'jack'
# 4- 增加元素--键不存在--新增--默认在尾部:python3
# dict1['weight'] = 150
# 5- in
# print('name' in dict1)
# 6- 删除  2种--没有remove
# del dict1['name']
# print(dict1.pop('name'))
# 7- 获取所有的键
# res = dict1.keys()#dict_keys(['name', 'age'])类列表:1- 不能用下标  2- 可以用for遍历
# # print(res)
# # for one in res:
# #     print(one)
#
# print(list(res))#转化成list---新对象--另存地址
# print(res)
# 8- 获取键值对
# print(dict1.items())
dict1 = {'name': 'tom', 'age': 20}
# for one in dict1:#对字典本身遍历只能是遍历的是键
#     print(one,dict1[one])
print(dict1.items())
for one, a in dict1.items():
    print(one, a)

# b , c = (10,20)
# print(b,c)


'''
字典定义:
    1- 键:#最常用:str
        1- 可以是:int str tuple float  不可以改变类型
            不可以是:list  dict      TypeError: unhashable type: 'list'
    2- 任意类型
特性:键名唯一性

'''
# students = {
#     'Mike Green': {
#   'age':25,
#      'height':180,
#      'weight':80,
#      'nickname':'ohmy'
#     },
#     'tom': {
#   'age':25,
#      'height':180,
#      'weight':80,
#      'nickname':'doggy'
#     }
# }
# print(students['tom']['age'])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值