-
寫一個函數(shù)將一個指定的列表中的元素逆序( 如[1, 2, 3] -> [3, 2, 1])(注意:不要使 表自帶的逆序函數(shù))
def reverse_list(list1: list): for index in range(0, len(list1)//2): list1[index], list1[len(list1)-1 - index] = list1[len(list1)-1 - index], list1[index] return list1 print(reverse_list([1, 2, 3, 4, 5])) print(reverse_list([1, 2, 3, 4])) 運行結(jié)果: [5, 4, 3, 2, 1] [4, 3, 2, 1]
-
寫一個函數(shù),提取出字符串中所有奇數(shù)位上的字符
def str_odd(str1:str): for i in range(len(str1)): if i % 2 == 0: print(str1[i], end=' ') str_odd('abcd') print() 運行結(jié)果: a c
-
寫一個匿名函數(shù)阁最,判斷指定的年是否是閏
is_leapyear = lambda year: year % 4 ==0 and year % 100 != 0 or year % 400 == 0 print(is_leapyear(1900)) print(is_leapyear(2000)) print(is_leapyear(2008)) 運行結(jié)果: False True True
-
寫函數(shù),提去字符串中所有的數(shù)字字符烦味。例如: 傳入'ahjs233k23sss4k' 返回: '233234'
def about_num(str1:str): str2 = '' for i in str1: if not 'a' <= i <='z' or 'A' <= i <= 'Z': str2 += i return str2 運行結(jié)果: 233234
-
寫一個函數(shù),獲取列表中的成績的平均值喊崖,和最高分
def gain_score(list1: list): sum1 = 0 max1 = 0 for s in list1: sum1 += s if max1 < s: max1 = s return sum1/len(list1), max1 print(gain_score([85, 85, 80, 90])) 運行結(jié)果: (85.0, 90)
-
寫函數(shù)涧偷,檢查獲取傳入列表或元組對象的所有奇數(shù)位索引對應(yīng)的元素第岖,并將其作為新的列表返回給調(diào)用者
def homework_six(item): list1 = [] for index in range(len(item)): if index % 2: list1.append(item[index]) return list1 print(homework_six((1, 2, 3, 4, 5, '6'))) print(homework_six(['asd', '789', 3, 4, 5, '6'])) 運行結(jié)果: [2, 4, '6'] ['789', 4, '6']
-
實現(xiàn)屬于自己的字典update方法:用一個字典去更新另一個字典的元素(不能使用自帶的update方法)yt_update(字典1潭流, 字典2)
def ff_update(dict1: dict, dict2: dict): for item2 in dict2: if item2 in dict1: dict1[item2] = dict2[item2] else: dict1[item2] = dict2[item2] return dict1 print(ff_update({'a':2, 'b': 3}, {'a': 1, 'b': 5, 'c': 6})) 運行結(jié)果: {'a': 1, 'b': 5, 'c': 6}
-
實現(xiàn)屬于自己的items方法:將字典轉(zhuǎn)換成列表竞惋,字典中的鍵值對轉(zhuǎn)換成元祖。(不能使用items方法)yt_items(字典),例如:{'a': 1, 'b':2, 'c':3} ---> [('a', 1), ('b', 2), ('c', 3)]
def ff_items(dict1: dict): list1 = [] for key in dict1: list1.append((key, dict1[key])) return list1 print(ff_items({'a': 1, 'b': 2, 'c': 3})) 運行結(jié)果: [('a', 1), ('b', 2), ('c', 3)]
-
有一個列表中保存的所一個班的學生信息灰嫉,使用max函數(shù)獲取列表中成績最好的學生信息和年齡最大的學生信息
all_student = [ {'name': '張三', 'age': 19, 'score': 90}, {'name': 'stu1', 'age': 30, 'score': 79}, {'name': 'xiaoming', 'age': 12, 'score': 87}, {'name': 'stu22', 'age': 29, 'score': 99} ] print(max(all_student, key=lambda x: x['score'])) print(max(all_student, key=lambda x: x['age'])) 運行結(jié)果: {'name': 'stu22', 'age': 29, 'score': 99} {'name': 'stu1', 'age': 30, 'score': 79}
Day10 作業(yè)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門贡这,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人厂榛,你說我怎么就攤上這事±霾眩” “怎么了击奶?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長责掏。 經(jīng)常有香客問我柜砾,道長,這世上最難降的妖魔是什么换衬? 我笑而不...
- 正文 為了忘掉前任痰驱,我火速辦了婚禮证芭,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘担映。我一直安慰自己废士,他們只是感情好,可當我...
- 文/花漫 我一把揭開白布蝇完。 她就那樣靜靜地躺著官硝,像睡著了一般。 火紅的嫁衣襯著肌膚如雪短蜕。 梳的紋絲不亂的頭發(fā)上氢架,一...
- 文/蒼蘭香墨 我猛地睜開眼解滓,長吁一口氣:“原來是場噩夢啊……” “哼赃磨!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起洼裤,我...
- 正文 年R本政府宣布,位于F島的核電站砚蓬,受9級特大地震影響矢门,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一祟剔、第九天 我趴在偏房一處隱蔽的房頂上張望隔躲。 院中可真熱鬧,春花似錦物延、人聲如沸宣旱。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽响鹃。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背瓶蚂。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 8月22日-----字符串相關(guān) 2-3 個性化消息: 將用戶的姓名存到一個變量中,并向該用戶顯示一條消息拉馋。顯示的消...