006、字典、元组的基本使用和方法

1、字典  {}

    字典里面,key相同,后面的key值会覆盖前面的值 ;

 a、查询

   students['gender'] 无该 key 则会报错 ;
   students.get('gender') 无该 key,返回None ;
      students.keys() 获取所有的key
      students.values() 获取所有的value
      students.items() 获取所有的key ,value
  
# key相同,则会覆盖前面的值;
students_1 = {'name': 'sky', 'age': 18, 'height': 170, 'age': 25}
print(students_1)

# 获取字典的值
students_2 = {'name': 'sky', 'age': 18, 'height': 170}
print(students_2['age'])
# print(students['gender'])       # 无该key,报错:KeyError: 'gender'

print(students_2.get('name'))
print(students_2.get('gender'))       # 无该key,返回None

print(students_2.keys())        # 获取所有的key
print(students_2.values())      # 获取所有的value
print(students_2.items())       # 获取所有的key ,value
View Code

执行结果如下:

D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day06/gggg/test_2.py
{'name': 'sky', 'age': 25, 'height': 170}
18
sky
None
dict_keys(['name', 'age', 'height'])
dict_values(['sky', 18, 170])
dict_items([('name', 'sky'), ('age', 18), ('height', 170)])

Process finished with exit code 0
View Code

 

    b、添加—修改

      字典[key] = value  :如果key存在,则修改值;如果key不存在,则添加值 ;
   可以添加的值有: 字典、列表、元组 等 ; key:一般是字符串 ;
      字典.setdefault(key,value)  :如果key存在,不会修改数据 ;如果key不存在,新建键值对 ;
      字典.update(字典2):将字典2的数据合并到字典1 ; 
students_3 = {'name': 'sky', 'age': 18, 'height': 170}
print(students_3)
students_3['weight'] = 75.3
print(students_3)

students_3['name'] = 'Tony'
print(students_3)

students_3['score'] = {'语文': 87, '数学': 78, '英语': 90}
print(students_3)

print('=========================')
student = {'name': 'sky', 'age': 18, 'height': 170}
score = {'语文': 87, '数学': 78, '英语': 90}
student.update(score)       # 将字典2的数据合并到字典1
print(student)

print('=========================')
student_1 = {'name': 'sky', 'age': 18, 'height': 170}
student_1.setdefault('name', 'Jane')        # 如果key存在,不会修改数据 ;
student_1.setdefault('hobby', '音乐')       # 如果key不存在,新建键值对 ;
print(student_1)
View Code

执行结果如下:

D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day06/gggg/test_2.py
{'name': 'sky', 'age': 18, 'height': 170}
{'name': 'sky', 'age': 18, 'height': 170, 'weight': 75.3}
{'name': 'Tony', 'age': 18, 'height': 170, 'weight': 75.3}
{'name': 'Tony', 'age': 18, 'height': 170, 'weight': 75.3, 'score': {'语文': 87, '数学': 78, '英语': 90}}
=========================
{'name': 'sky', 'age': 18, 'height': 170, '语文': 87, '数学': 78, '英语': 90}
=========================
{'name': 'sky', 'age': 18, 'height': 170, 'hobby': '音乐'}

Process finished with exit code 0
View Code

    c、删除

     students_1.pop('age')     # 删除指定键值对,必须要传一个key,否则会报错;

     del students_1['height']      # 用del粗暴的回收 ;

     print(students_1.popitem())    # 返回并删除字典中的最后一对键和值;

     students_1.clear()        # 清空字典;

students_1 = {'name': 'sky', 'age': 18, 'height': 170, 'weight': 65, 'hobby': '音乐', 'phone': '荣耀'}
print(students_1)

# students_1.pop()        # 报错,无该方法,与列表有点不一样

students_1.pop('age')      # 删除指定键值对
print(students_1)

del students_1['height']   # 用del粗暴的回收
print(students_1)

print(students_1.popitem())       # 返回并删除字典中的最后一对键和值。
print(students_1)

students_1.clear()        # 清空字典
print(students_1)
View Code

执行结果如下:

D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day06/gggg/test_2.py
{'name': 'sky', 'age': 18, 'height': 170, 'weight': 65, 'hobby': '音乐', 'phone': '荣耀'}
{'name': 'sky', 'height': 170, 'weight': 65, 'hobby': '音乐', 'phone': '荣耀'}
{'name': 'sky', 'weight': 65, 'hobby': '音乐', 'phone': '荣耀'}
('phone', '荣耀')
{'name': 'sky', 'weight': 65, 'hobby': '音乐'}
{}

Process finished with exit code 0
View Code

 

  d、获取字典的键值对的数量

 len(字典)

students = {'语文': 87, '数学': 78, '英语': 90}
print(len(students))        # 获取字典的键值对的数量

执行结果如下:

D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day06/gggg/test_2.py
3

Process finished with exit code 0

 

e、 成员 in  运算

student = {'name': 'sky', 'age': 18, 'height': 170}

print('name' in student)        # 成员in时,student 默认是 key
print('sky' in student)
print('name' in student.keys())     # 等同于 print('name' in student)

print('sky' in student.values())
print(('name', 'sky') in student.items())
View Code

执行结果如下:

D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day06/gggg/test_2.py
True
False
True
True
True

Process finished with exit code 0
View Code

 

 

2、元组  ()  

  与列表有很多相同的地方,有序有索引;

  元组:不可变数据类型,只可读,不可对它修改 。 列表 :可读、可写、可修改;

  读取元素:元组[index]  、切片 ,  长度: len(元组);  

t = (1, 2, 3, 5, 4)
print(t)
print(t[1])
print(t[::-1])
print(t[1: 3])
print(len(t))

# 定义一个元素的元组
t1 = (1,)
print(t1)

print('=========================')
# 元组的第二种定义
t2 = 1, 2, 3
print(t2)
View Code

执行结果如下:

D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day06/gggg/test_2.py
(1, 2, 3, 5, 4)
2
(4, 5, 3, 2, 1)
(2, 3)
5
(1,)
=========================
(1, 2, 3)

Process finished with exit code 0
View Code

 

 

扩展知识:

  常用的数据类型:

  可变数据类型:int 、float、str、bool、tuple

  可变数据类型:list、字典

 

posted @ 2021-07-25 17:32  空-山-新-雨  阅读(74)  评论(0编辑  收藏  举报