Day008 - 字典与集合

字典(dict)

字典元素的增删改

元素的增加和修改

字典[键] = 值
  • 当键存在的时候是修改键对应的值,当键不存在的时候是添加键对;

  • 例子

    cat = {'name': '美美', 'age': 2, 'color': '白色'}
    print(cat)  # {'name': '美美', 'age': 2, 'color': '白色'}
    # 添加
    cat['breed'] = '蓝猫'
    print(cat)  # {'name': '美美', 'age': 2, 'color': '白色', 'breed': '蓝猫'}
    # 修改
    cat['age'] = 5
    print(cat)  # {'name': '美美', 'age': 5, 'color': '白色', 'breed': '蓝猫'}
    
字典.setdefault(键,值)
  • 添加键对,键不存在的时候添加键值对,存在的时候不修改键值对;

  • 例子

    cat.setdefault('weight', 8)
    cat.setdefault('age', 1)
    print(cat)  # {'name': '美美', 'age': 5, 'color': '白色', 'breed': '蓝猫', 'weight': 8}
    
练习
  • 在students中没有分数的学生中添加分数对应的键值对,分数值为零
students = [
    {'name': 'stu1', 'tel': '1234', 'score': 89},
    {'name': 'stu2', 'tel': '465', 'score': 80},
    {'name': 'stu3', 'tel': '678'},
    {'name': 'stu3', 'score': 78},
    {'name': 'stu4', 'tel': '234'}
]
for item in students:
    item.setdefault('score', 0)
    print(item)
# 结果
[{'name': 'stu1', 'tel': '1234', 'score': 89}, 
 {'name': 'stu2', 'tel': '465', 'score': 80}, 
 {'name': 'stu3', 'tel': '678', 'score': 0}, 
 {'name': 'stu3', 'score': 78}, 
 {'name': 'stu4', 'tel': '234', 'score': 0}]
{

字典元素的删除

del 字典[键]
  • 删除指定键对应的键值对,键不存在会报错KeyError

  • 例子

    print(cat)  # {'name': '美美', 'age': 5, 'color': '白色', 'breed': '蓝猫', 'weight': 8}
    del cat['color']
    print(cat)  # {'name': '美美', 'age': 5, 'breed': '蓝猫', 'weight': 8}
    # del cat['III']  # KeyError
    
字典.pop(键)
  • 取出字典中指定键对应的值

  • 例子

    del_value = cat.pop('breed')  # {'name': '美美', 'age': 5, 'breed': '蓝猫', 'weight': 8}
    print(del_value)  # 蓝猫
    # cat.pop('III')  # KeyError
    

字典相关操作与函数

相关操作

  • 字典不支持比较大小运算,不支持+、*;只支持==、!=
in与not in
  • 字典的in和not in 判断的是键是否存在

  • 键 in 字典

  • 键 not in 字典

  • 例子

    dic1 = {'a': 10, 'b': 20, 'c': 30}
    print(10 in dic1)  # False
    print('a' in dic1)  # True
    

相关函数

len
  • 获取字典长度
dict(数据)
  • 数据转换成字典类型

  • 数据要求

    1. 数据本身是一个序列
    2. 数据中的元素必须是有且只有两个元素小序列
    3. 小序列的第一个元素必须不可变数据
  • 例子

    data = [('a', 10), ('b', 20)]
    print(dict(data))  # {'a': 10, 'b': 20}
    
    data2 = ['ab', range(2), [10, 20]]
    print(dict(data2))  # {'a': 'b', 0: 1, 10: 20}
    
字典转换成非字典
转换成列表
dic3 = {'a': 10, 'b': 20, 'c': 30}
print(list(dic3))  # ['a', 'b', 'c']
转换成元组
dic3 = {'a': 10, 'b': 20, 'c': 30}
print(tuple(dic3))  # ('a', 'b', 'c')

字典的相关方法

字典.clear()

  • 清空字典

字典.copy()

  • 复制原字典产生一个一模一样的新字典;

字典.update(序列)

  • **将字典或者能转化成字典的序列中的所有元素添加到字典中,如果元素键存在则覆盖值; **

    dic4 = {'d': 100, 'e': 200}
    print(dic3, dic4)  # {'a': 10, 'b': 20, 'c': 30} {'d': 100, 'e': 200}
    dic3.update(dic4)
    print(dic3)  # {'a': 10, 'b': 20, 'c': 30, 'd': 100, 'e': 200}
    dic4.update(['xx', 'yy'])
    print(dic4)  # {'d': 100, 'e': 200, 'x': 'x', 'y': 'y'}
    dic5 = {'a': 10000, 'b': 20000}
    print(dic3, dic5)  # {'a': 10, 'b': 20, 'c': 30, 'd': 100, 'e': 200} {'a': 10000, 'b': 20000}
    dic3.update(dic5)
    print(dic3)  # {'a': 10000, 'b': 20000, 'c': 30, 'd': 100, 'e': 200}
    

字典.keys()

  • 返回字典的键

    dic4 = {'d': 100, 'e': 200, 'x': 'x', 'y': 'y'}
    print(dic4.keys())  # dict_keys(['d', 'e', 'x', 'y'])
    

字典.values()

  • 返回字典的值

    dic4 = {'d': 100, 'e': 200, 'x': 'x', 'y': 'y'}
    print(dic4.values())  # dict_values([100, 200, 'x', 'y'])
    

字典.items()

  • 返回字典的键值对

    dic4 = {'d': 100, 'e': 200, 'x': 'x', 'y': 'y'}
    print(dic4.items())  # dict_items([('d', 100), ('e', 200), ('x', 'x'), ('y', 'y')])
    

字典推导式

  • {键的表达式:值的表达式 for 变量 in 序列}
    
  • {键的表达式:值的表达式 for 变量 in 序列 if 条件语句}
    
  • 练习:通过字典的推导式交换一个字典的键和值

    dict1 = {'a': 10, 'b': 20}
    a = {v: k for k, v in dict1.items()}
    print(a)
    

集合(set)

集合定义

  • 集合是容器
  • 将**{}作为容器标志**,多个元素用逗号隔开:{元素1,元素2,…,元素N}
  • 集合是可变的且无序的;
  • 元素的要求:不可变类型的数据元素是唯一的(具备自动去重功能)

空集合

  • set1 = set()
    print(type(set1), len(set1))  # <class 'set'> 0
    

集合的无序

  • print({1, 2, 3} == {3, 2, 1})  # True
    

集合元素不可变性

  • print({4, 5, 6})  # {4, 5, 6}
    # print({4, 5, [6, ]})  # 报错TypeError: unhashable type: 'list'
    

集合元素的唯一性

  • 自动去掉重复

  • print({1, 2, 3, 1, 2, 3, 4, 5})  # {1, 2, 3, 4, 5}
    

集合元素的增删改查

元素的查询

遍历
for 元素 in 集合:
    循环体

元素的增加

集合.add(元素)
  • 在集合中添加指定元素

  • 例子

    nums = {34, 52, 45, 78, 543}
    nums.add(999)
    print(nums)  # {34, 999, 45, 78, 52, 543}
    
集合.update(序列)
  • 将序列中的元素全部添加到集合中

  • 例子

    nums = {34, 52, 45, 78, 543}
    nums.add(999)
    print(nums)  # {34, 999, 45, 78, 52, 543}
    nums.update('abc')
    print(nums)  # {34, 'c', 999, 'b', 45, 78, 52, 'a', 543}
    

元素的删除

集合.remove(元素)
  • 删除指定元素,元素不存在会报错

  • 例子

    nums = {34, 'c', 999, 'b', 45, 78, 52, 'a', 543}
    nums.remove(45)
    # nums.remove(666)  # KeyError
    print(nums)  # {'b', 34, 'c', 999, 'a', 78, 52, 543}
    

集合.discard(元素)

  • 删除指定元素,元素不存在不会报错

    nums.discard(52)
    nums.discard(666)  # 不会报错
    print(nums)  # {34, 'c', 999, 78, 'a', 'b', 543}
    

元素的修改

  • 集合无法直接修改元素,如果一定要改只有删除元素后再添加

  • 例子

    print(nums)  # {34, 999, 'b', 78, 'a', 'c', 543}
    nums.remove('a')
    nums.add('A')
    print(nums)  # {34, 999, 'A', 78, 'c', 'b', 543}
    

数学集合运算

  • 交集(&)、并集(|)、差集(-)、对称差集(^)、真子集(>/<)、子集(>=/<=)
nums1 = {1, 2, 3, 4, 5, 6, 7}
nums2 = {5, 6, 7, 8, 9}

交集(&) - 集合1 & 集合2

  • 获取两个集合的公共元素(获取既在集合1又在集合2的元素)

    print(nums1 & nums2)  # {5, 6, 7}
    

并集(|) - 集合1 | 集合2

  • 获取两个集合所有的元素

    print(nums1 | nums2)  # {1, 2, 3, 4, 5, 6, 7, 8, 9}
    

差集(-) - 集合1 - 集合2

  • 获取被减集合中包含在减去集合元素以外的部分

    print(nums1 - nums2)  # {1, 2, 3, 4}
    print(nums2 - nums1)  # {8, 9}
    

对称差集(^) - 集合1 ^ 集合2

  • 将两个集合合并,去掉公共部分之后剩余的元素

    print(nums1 ^ nums2)  # {1, 2, 3, 4, 8, 9}
    

真子集(>/<)、子集(>=/<=)

  • 子集是包括本身的元素的集合,可以等于集合本身

  • 真子集是除本身集合的元素的集合,真子集一定比该集合小

    print({10, 20, 30, 40} > {1, 2})  # False
    print({10, 20, 30, 40} > {10, 40})  # True
    print({10, 20, 30, 40} > {10, 20, 30, 40})  # False
    print({10, 20, 30, 40} >= {10, 20, 30, 40})  # True
    

字符串基础

  • 字符串是不可变的、有序的(支持下标);

字符串的有序性

print('abc' == 'cba')  # False
str1 = 'abcde'
print([i for i in str1])  # ['a', 'b', 'c', 'd', 'e']
print([str1[index1] for index1 in range(len(str1))])  # ['a', 'b', 'c', 'd', 'e']
print(str1[1:])  # bcde
print(str1[:-2])  # abc
print(str1[::2])  # ace

字符串相关操作

+、*

str1, str2 = 'abcde', 'defg'
print(str1 + str2)  # abcdedefg
print(str1 * 2)  # abcdeabcde

比较运算

str1, str2 = 'abcde', 'defg'
print('abcde' >= 'abc')  # True
print('abcde' >= 'abcf')  # False

in与not in

str1, str2 = 'abcde', 'defg'
print('a' in 'abcde')  # True
print('f' not in 'abcde')  # True

len与str

print(len('abcdefgh'))  # 8
num = 12345
num1 = str(num)
print(num1, type(num1))  # 12345 <class 'str'>

课后习题

  1. 定义一个列表,在列表中保存6个学生的信息(学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) )

    stu_info = [
        {'name': '赵一', 'age': 17, '生物': 59, 'tel': '13111111111', 'gender': '男'},
        {'name': '钱二', 'age': 18, '生物': 61, 'tel': '13222222222', 'gender': '女'},
        {'name': '孙三', 'age': 19, '生物': 99, 'tel': '13333333333', 'gender': '不明'},
        {'name': '李四', 'age': 20, '生物': 78, 'tel': '13444444444', 'gender': '女'},
        {'name': '周五', 'age': 19, '生物': 30, 'tel': '13555555555', 'gender': '不明'},
        {'name': '吴六', 'age': 18, '生物': 87, 'tel': '13666666668', 'gender': '男'}
    ]
    
    1. 统计不及格学生的个数

      count_no_pass = 0
      for stu in stu_info:
          if stu['生物'] < 60:
              count_no_pass += 1
      print('不及格学生的个数:', count_no_pass)
      # 不及格学生的个数: 2
      
    2. 打印不及格学生的名字和对应的成绩

      for stu in stu_info:
          if stu['生物'] < 60:
              print(stu['name'], stu['生物'])
      # 赵一 59
      # 周五 30
      
    3. 打印手机尾号是8的学生的名字

      for stu in stu_info:
          if stu['tel'][-1] == '8':
              print(stu['name'])
      # 吴六
      
    4. 打印最高分和对应的学生的名字

      max_score, max_name, = stu_info[0]['生物'], [stu_info[0]['name']]
      for stu in stu_info[1:]:
        	tmp_score, tmp_name = stu['生物'], stu['name']
        	if tmp_score > max_score:
        		max_score = tmp_score
        		max_name.clear()
        		max_name.append(tmp_name)
        	elif tmp_score == max_score:
        		max_name.append(tmp_name)
        print(max_name, max_score)
      # 孙三 99
      
    5. 删除性别不明的所有学生

      for index1, stu in enumerate(stu_info.copy()):
          if stu['gender'] == '不明':
              del stu_info[index1]
      print(stu_info)
      
    6. 将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)

      lst, lst2 = [], stu_info.copy()
      for index1, stu in enumerate(stu_info):
          lst.append((stu['生物'], index1))
      lst.sort(reverse=True)
      for index2 in range(len(stu_info)):
          stu_info[index2] = lst2[lst[index2][1]]
      print(stu_info)
      
  2. 用三个集合表示三门学科的选课学生姓名(一个学生可以同时选多门课)

    math = {'南莲', '紫翠', '雨寒', '慕灵', '以蕊', '雪柳', '孤岚', '笑霜', '海云'}
    phy = {'易烟', '如萱', '若南', '慕灵', '以蕊', '寻雁', '映易', '笑霜', '海云'}
    chem = {'寻真', '晓亦', '向珊', '寻雁', '映易', '雪柳', '孤岚', '笑霜', '海云'}
    
    1. 求选课学生总共有多少人

      sum1 = math | phy | chem
      print('选课学生总共有:', len(sum1), '人')
      # 选课学生总共有: 17 人
      
    2. 求只选了第一个学科的人的数量和对应的名字

      only_math = math - (phy | chem)
      print(len(only_math), only_math)
      # 3 {'紫翠', '南莲', '雨寒'}
      
    3. 求只选了一门学科的学生的数量和对应的名字

      only_one = (math | phy | chem) - (math & phy) - (math & chem) - (phy & chem)
      # 9 {'紫翠', '易烟', '如萱', '寻真', '晓亦', '南莲', '向珊', '若南', '雨寒'}
      
    4. 求只选了两门学科的学生的数量和对应的名字

      only_two = ((math & phy) | (math & chem) | (phy & chem)) - (math & phy & chem)
      print(len(only_two), only_two)
      # 6 {'孤岚', '雪柳', '以蕊', '映易', '寻雁', '慕灵'}
      
    5. 求选了三门学生的学生的数量和对应的名字

      only_three = math & phy & chem
      print(len(only_three), only_three)
      # 2 {'笑霜', '海云'}
      

作业2

  1. 控制台学生管理系统
def welcome():
    """主界面"""
    long_1 = ' ' * len('✿✿✿欢迎Soft:♥')
    print('=' * 50)
    print('✿✿✿欢迎Soft:')
    print()
    print(long_1, '♥ 1. 添加学生')
    print(long_1, '♥ 2. 查看学生')
    print(long_1, '♥ 3. 修改学生信息')
    print(long_1, '♥ 4. 删除学生')
    print(long_1, '♥ 5. 返回')
    print('=' * 50)


def append_student():
    """添加学生"""
    numbers1 = 'stu' + str(len(stu_info) + 1).zfill(4)
    name1 = input('请输入姓名:')
    age1 = input('请输入年龄:')
    tel1 = input('请输入电话:')
    stu_info.append({'number': numbers1, 'name': name1, 'age': age1, 'tel': tel1})


def find_all_student():
    """查找所有学生"""
    for stu in stu_info:
        print(stu['number'], stu['name'], stu['age'], stu['tel'])


# 学生信息存储
stu_info = [
    # {'number': 'stu0001', 'name': 111, 'age': 22, 'tel': '112'}
]

while True:
    welcome()
    input_choice = input('请选择(1-5):')
    if input_choice == '1':  # 添加学生分支
        while True:
            long1 = len(stu_info)
            append_student()
            if long1 + 1 == len(stu_info):
                print('添加成功!', '1.继续', '2.返回', sep='\n')
                input_choice1 = input('请选择(1-2):')
                if input_choice1 == '1':
                    continue
                elif input_choice1 == '2':
                    print('返回成功!')
                    break
                else:
                    print('输入错误,已返回主界面!')
                    break
    elif input_choice == '2':  # 查找学生分支
        if not len(stu_info):
            print('该账号没有可管理的学生!请先添加学生!')
            continue
        elif len(stu_info):
            while True:
                print('=' * 25, '1.查找所有学生', '2.根据姓名查找', '3.根据学号查找', '4:返回', '=' * 25, sep='\n')
                input_choice2 = input('请选择(1-5):')
                if input_choice2 == '1':  # 1.查找所有学生
                    find_all_student()
                elif input_choice2 == '2':  # 2.根据姓名查找
                    pass
                elif input_choice2 == '3':  # 3.根据学号查找
                    print('无')
                    continue
                elif input_choice2 == '4':
                    print('返回成功!')
                    break
                else:
                    print('输入错误,请重新输入')
                    continue

    elif input_choice == '3':  # 修改学生信息分支-未完成
        print('无')
        continue

    elif input_choice == '4':  # 删除学生信息分支-未完成
        print('无')
        continue
    elif input_choice == '5':
        print('返回成功!')
        break
    else:
        print('输入错误,已返回主界面!')
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值