python第二周day3
python第二周day3(9.24)
1、day7字典作業更改版
# 定義一個列表,在列表中保存6個學生的信息(學生信息中包括: 姓名、年齡、成績(單科)、電話、性別(男、女、不明) ) students = [{'name': '晨晨', 'age':18, 'score': 78, 'tel': '123', 'gender': '男'},{'name': '陳來', 'age':20, 'score': 80, 'tel': '321', 'gender': '不明'},{'name': '陳昕', 'age':28, 'score': 98, 'tel': '653', 'gender': '女'},{'name': '小新', 'age':32, 'score': 65, 'tel': '783', 'gender': '男'},{'name': '小明', 'age':17, 'score': 24, 'tel': '988', 'gender': '女'},{'name': '小紅', 'age':14, 'score': 54, 'tel': '903', 'gender': '男'} ] # 1. 統計不及格學生的個數 count = 0 for stu in students:if stu['score'] < 60:count += 1 print('1)不及格學生的個數:', count) # 2 # 2.打印不及格學生的名字和對應的成績 print('2)不及格學生的名字和對應的成績') for stu in students:score = stu['score'] #先保存成績數據if score < 60:print(stu['name'], score) # 小明 24 小紅 54 # 3.統計未成年學生的個數 # **4.打印手機尾號是8的學生的名字 # 方法一 print('4)手機尾號是8的學生的名字:') for stu in students:if stu['tel'][-1] == '8':print(stu['name']) # 小明 # 方法二 for stu in students:if int(stu['tel']) % 10 == 8:print(stu['name']) # 小明 # 5)打印最高分和對應的學生的名字 print('5)最高分和對應的學生的名字:') # 方法一: # **第一次循環獲取最高分 max_score = students[0]['score'] for stu in students[1:]:score = stu['score']if score > max_score:max_score = score # 第二次循環找到分數和最高分相等的所有學生的姓名 for stu in students:if stu['score'] == max_score:print(stu['name'], max_score) # 陳昕 98# **方法二: max_score = students[0]['score'] names = [students[0]['name']] for stu in students[1:]:score = stu['score']if score == max_score:names.append(stu['name'])elif score > max_score:max_score = scorenames.clear()names.append(stu['name']) print(names, max_score) # ['陳昕'] 98# 6.刪除性別不明的所有學生(使用提出) 老師舉例(下標遍歷--倒著取) #方法一(刪除) print('6)刪除性別不明的所有學生:') # for stu in students[:]: # if stu['gender']== '不明': # students.remove(stu) # print(students) # 方法二,推導式(提取) # new_students = [stu for stu in students if stu['gender']!= '不明'] # print(new_students)#舉例說明 # nums = [89,90, 10, 11, 30, 60] # temp = nums.copy() # for x in nums[:]:#產生一個新的一樣的nums ,相當于temp ,nums.copy() 遍歷一個刪除另外一個 # if x < 60: # nums.remove(x) # print(nums)# 7.將列表按學生成績從大到小排序 students.sort(key=lambda item: item['score'], reverse=True) print(students)# 用三個集合表示三門學科的選課學生姓名(一個學生可以同時選多門課) math = {'陳來', '小張', '小溪'} history = {'陳來', '小新', '小智', '小張'} english = {'小李', '小麗', '小智', '小張'} # 1. 求選課學生總共有多少人 s1 = math | history | english print(s1, '選課學生人數:', len(s1)) # 2. 求只選了第一個學科的人的數量和對應的名字 s2 = math - history - english print('只選了第一個學科的人的數量:', len(s2)) print('只選了第一個學科的名字:', s2) # 3. 求只選了一門學科的學生的數量和對應的名字 # 方法一 s3 = (math - history - english) | (history - math - english) | (english - history - math) print('只選了一門學科的學生的數量:', len(s3)) print('只選了一門學科的學生的名字:', s3) # 方法二 s3 =(math ^ history ^ english) - (math & history & english) print(s3) # 5. 求選了三門學生的學生的數量和對應的名字 math = {'陳來', '小張', '小溪'} history = {'陳來', '小新', '小智', '小張'} english = {'小李', '小麗', '小智', '小張'} s5 = math & history & english print('選了三門學生的學生的數量:', len(s5)) print('選了三門學生的學生的名字:', s5)# 4. 求只選了兩門學科的學生的數量和對應的名字 # result3 = math & history & english # result1 = (math & history ) | (math & english) | (english & history) # result4 = result1 - result3 # print(result4, len(result4)) s4 =s1 - s3 - s5 print('只選了兩門學科的學生的數量:', len(s4)) print('只選了兩門學科的學生的名字:', s4)2、字典相關操作和方法
1.字典不支持 +、*、<,>,<=,>= 比較大小
2.字典支持: == 、!=
print({'a': 10, 'b': 20} == {'b': 20, 'a': 10}) # True3.in 和 not in
“”"
鍵 in 字典 - 判斷字典中是否存在指定的鍵
“”"
d1 = {'a': 10, 'b': 20, 'c': 30} print(30 in d1) # False print('b' in d1) # True4.dict(類型轉換)
“”"
dict(數據) - 將數據轉換成字典
數據的要求:1)數據本身是一個序列
2)序列中的元素必須是長度為2的小序列(鍵值對)
3)小序列的第一個元素必須是不可變的數據
“”"
注意:將字典轉換成理部或者元組的時候,將字典的鍵作為列表,元組中的元素
result = dict([(1, 2), 'ab', ['a', 100]]) # 轉換為字典 print(result) # {1: 2, 'a': 100} 重復了保存一個 result = dict([(1, 2), 'bb', ['a', 100]]) print(result) # {1: 2, 'b': 'b', 'a': 100}# 注意:將字典轉換成理部或者元組的時候,將字典的鍵作為列表,元組中的元素 d1 = {'a': 10, 'b': 20, 'c': 30} print(list(d1)) # 轉換的是鍵 ['a', 'b', 'c']t2 = ((1, 2), 'a1', [3, 9]) result = dict(t2) print(result) # {1: 2, 'a': '1', 3: 6}5.相關方法
1)字典.clear() - 清空字典
{‘name’: ‘小明’, ‘gender’: ‘男’, ‘age’: 18} -> [(‘name’, ‘小明’)]
{鍵1:值1,鍵2:值2} ->[(鍵1,值1),(鍵2,值2)]
4)字典.keys() - 獲取字典所有的鍵,返回一個新的序列
d2 = {'a': 2, 'b': 34, 'c': 21} print(d2.keys()) # dict_keys(['a', 'b', 'c'])5)字典.values() - 獲取字典所有的值,返回一個新的序列
print(d2.values()) # dict_values([2, 34, 21])6)字典.setdefault(鍵,值) - 在字典中添加一個鍵值對(如果字典中已經存在鍵值對,不會執行修改操作)
字典[鍵] = 值
d1 = {'a': 10} print(d1) # {'a': 10}d1['b'] = 20 print(d1) # {'a': 10, 'b': 20}d1.setdefault('c', 30) print(d1) # {'a': 10, 'b': 20, 'c': 30}d1 = {'a': 10} d1.setdefault('a', 100) print(d1) # {'a': 10}d1['a'] = 100 print(d1) # {'a': 100} d3 = {'a': 10, 'b': 20, 'c': 30} d3.setdefault('d',190) print(d3)goods_list = [{'name': '泡面', 'price': 4, 'discount': 0.9, 'count': 100},{'name': '火腿腸', 'price': 2, 'count': 120},{'name': '礦泉水', 'price': 1, 'count': 500},{'name': '面包', 'price': 5, 'count': 120, 'discount': 0.75} ] for goods in goods_list:goods.setdefault('discount', 1) print(goods_list) # 直接添加'discount': 1for goods in goods_list:goods['discount'] = 1 print(goods_list)7)字典.update(序列) - 將序列中的元素全部添加到字典中
序列 - 是字典或者是能轉換成字典的序列
d1 = {'a': 10, 'b': 20} d2 = {'name': '小明', 'age': 18, 'a': 200} d1.update(d2) print(d1) # {'a': 200, 'b': 20, 'name': '小明', 'age': 18} d1.update(['mn', (1, 2)]) print(d1) # {'a': 200, 'b': 20, 'name': '小明', 'age': 18, 'm': 'n', 1: 2}3、集合
1.什么是集合(set)
“”"
集合是容器型數據類型(序列);將{}作為容器的標志里面多個元素用逗號隔開:{元素1,元素2,元素3…}
集合是可變的;集合無序的
元素 - 必須是不可變的數據;唯一
“”"
3)集合無序
print({1, 2, 3} == {3, 2, 1}) # True(順序不影響結果)4)元素是唯一的
s3 = {1, 2, 3, 1, 1, 4} print(s3) # {1, 2, 3, 4} names = ['張三', '張三', '李思', '小明'] print(set(names)) # {'李思', '小明', '張三'} 去重,數據2.集合的增刪改查(不支持改,刪掉和增加,了解)
1) 查 - 只能遍歷(集合無序)
hobby = {'玩游戲', '看電影', '打籃球', '爬山', '做飯'} for x in hobby:print(x)2)增:
集合.add(元素)
集合.update(序列)
集合.remove(元素) - 如果元素不存在會報錯
集合.discard(元素) - 如果元素不存在不會報錯
4、數學集合運算
python中的集合支持數學集合運算
1.并集 - |
集合1 |集合2 - 將兩個集合合并成一個集合
2.交集 - &
集合1 & 集合2 - 獲取兩個集合的公共部分,產生一個新的集合
result = s1 & s2 print(result) # {3, 4}3.差集 - -
集合1 - 集合2 - 獲取集合1中去掉包含在集合2中的元素,剩下的元素
s1 = {1, 2, 3, 4, 5} s2 = {3, 4, 6, 7, 8} result = s1 - s2 print(result) # {1, 2, 5}4.對稱差集 - ^ -A并B減去A交B
集合1 ^ 集合2 -將兩個集合合并,去掉公共部分獲取剩下的部分
print(s1^s2) # {1, 2, 5, 6, 7, 8}5.判斷子集關系
子集: >= , <=
真子集: >, <
集合1 > 集合2 - 判斷集合2是否是集合1的真子集
集合1 < 集合2 - 判斷集合1是否是集合2的真子集
總結
以上是生活随笔為你收集整理的python第二周day3的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Skywalking使用与探针机制
- 下一篇: 数字后端基本概念介绍Tie cell