日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python第二周day3

發布時間:2024/3/12 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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}) # True

3.in 和 not in
“”"
鍵 in 字典 - 判斷字典中是否存在指定的鍵

“”"

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

4.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() - 清空字典

d1.clear() print(d1)
  • 字典.copy() -
  • stu1 = {'name': '小明', 'gender':'男', 'age': 18} stu2 = stu1.copy() print(stu2) stu2['name'] = '張三' print(stu2, stu1)# stu3 = stu1 # print(stu3) # stu3['name']= '李思' # print(stu3, stu1)
  • 字典.items() - 將字典中的元素轉換成元組,返回一個新的序列
    {‘name’: ‘小明’, ‘gender’: ‘男’, ‘age’: 18} -> [(‘name’, ‘小明’)]
    {鍵1:值1,鍵2:值2} ->[(鍵1,值1),(鍵2,值2)]
  • 字典推導式:{鍵值對 for 變量 in 序列}、{鍵值對 for 變量 in 序列 if 條件}
  • stu1 = {'name': '小明', 'gender':'男', 'age': 18} result = stu1.items() print(result) # dict_items([('name', '小明'), ('gender', '男'), ('age', 18)]) print(list(result)) # 任何序列都可以轉換成列表 [('name', '小明'), ('gender', '男'), ('age', 18)]# 練習:使用列表推導式,將一個字典的值全部乘以10 d2 = {'a': 2, 'b': 34, 'c': 21} # {'a': 20, 'b': 340, 'c': 210} d3 = dict([(x, y*10) for x, y in d2.items()]) # [(x,y),(x1,y1)] print(d3) # {'a': 20, 'b': 340, 'c': 210}# 字典推導式:{鍵值對 for 變量 in 序列}、{鍵值對 for 變量 in 序列 if 條件} d2 = {'a': 2, 'b': 34, 'c': 21} new_d2 = {x: d2[x]*10 for x in d2} print(new_d2) # {'a': 20, 'b': 340, 'c': 210}x = (10, 20) x, y = (10, 20) d3 = {key: value*10 for key,value in d2.items()} # 轉換為序列-->元組 print(d3) # {'a': 20, 'b': 340, 'c': 210}# 練習2:使用推導式,交換字典的鍵和值 d2 = {'a': 2, 'b': 34, 'c': 21} # new_d2 = {d2[x]: x for x in d2} print(new_d2) # {2: 'a', 34: 'b', 21: 'c'} new_d2 = {value: key for key, value in d2.items()} print(new_d2) new_d2 = dict([(y, x) for x, y in d2.items()]) print(new_d2)

    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…}
    集合是可變的;集合無序的
    元素 - 必須是不可變的數據;唯一
    “”"

  • 空集合
  • s1 = set() print(len(s1), type(s1)) # 0 <class 'set'>
  • 集合元素必須是不可變的數據 (集合無序)
  • s2 = {1, 'abc', (23,4)} # 數字,字符串,元組不可變數據 print(s2) # {(23, 4), 1, 'abc'}

    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(序列)

    hobby.add('游泳') print(hobby) # {'看電影', '游泳', '爬山', '做飯', '玩游戲', '打籃球'} hobby.update(('乒乓球', '羽毛球')) print(hobby) # {'玩游戲', '做飯', '游泳', '羽毛球', '爬山', '乒乓球', '打籃球', '看電影'}
  • 集合.remove(元素) - 如果元素不存在會報錯
    集合.discard(元素) - 如果元素不存在不會報錯

    hobby.remove('做飯') print(hobby) # {'羽毛球', '爬山', '看電影', '乒乓球', '玩游戲', '打籃球', '游泳'} hobby.discard('玩游戲') print(hobby) # {'游泳', '看電影', '乒乓球', '打籃球', '爬山', '羽毛球'}# hobby.remove('做飯') hobby.discard('做飯')

    4、數學集合運算

    python中的集合支持數學集合運算
    1.并集 - |
    集合1 |集合2 - 將兩個集合合并成一個集合

    s1 = {1, 2, 3, 4, 5} s2 = {3, 4, 6, 7, 8} result = s1 | s2 print(result) # {1, 2, 3, 4, 5, 6, 7, 8}

    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的真子集

    s3 = {10, 20, 30} # 真子集: {10, 20},{20,30},{10,30},{10},{20},{30},空集 # 子集: {10, 20},{20,30},{10,30},{10},{20},{30},空集,{10, 20, 30} print({9, 2, 3} > {3, 4}) # False 判斷真子集 print({4, 2, 3} > {3, 4}) # True 是真子集 # 練習 # 用三個集合表示三門學科的選課學生姓名(一個學生可以同時選多門課) 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)

    總結

    以上是生活随笔為你收集整理的python第二周day3的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。