day10 作業(yè)

#1.寫(xiě)一個(gè)函數(shù)將一個(gè)指定的列表中的元素逆序(如[1, 2, 3] -> [3, 2, 1])(注意:不要使  表自帶的逆序函數(shù))
def nx_list(list):
      list1 = list[::-1]
      print(list1)
list = [1, 2, 3, 4]
nx_list(list)
#2.寫(xiě)一個(gè)函數(shù),提取出字符串中所有奇數(shù)位上的字符
def js_str(str2):
     str1 = str2[::2]
     print(str1)

str2 =  'zzzcxccsdad'
js_str(str2)
#3.寫(xiě)一個(gè)匿名函數(shù),判斷指定的年是否是閏
# zzz = lambda year:'閏年' if n % 4 == 0 and n % 100 != 0  else '非閏年'
# n = 2012
# print(zzz(n))
#5.寫(xiě)函數(shù), 利用遞歸獲取斐波那契數(shù)列中的第10個(gè)數(shù),并將該值返回給調(diào)用者。
#6.寫(xiě)一個(gè)函數(shù),獲取列表中的成績(jī)的平均值,和最高分
def ave_max(list):
    zs = 0
    for pjs in list:
        zs += pjs
        ave = zs/len(list)
    print(ave)
    max1 = max(list)
    print(max1)
n1 = [1, 2, 3, 4]
ave_max(n1)
#7.寫(xiě)函數(shù),檢查獲取傳入列表或元組對(duì)象的所有奇數(shù)位索引對(duì)應(yīng)的元素,并將其作為新的列表返回給調(diào)用者

def js(listn):
     index2 = []
     for index1 in listn[1::2]:
        index2.append(index1)
     print(index2)
n = [1, 2, 3, 4]
js(n)
#8.實(shí)現(xiàn)屬于自己的字典update方法:用一個(gè)字典去更新另一個(gè)字典的元素(不能使用自帶的update方法)yt_update(字典1, 字典2)
def update_dict1(basic_dict1,new_dict1):
    for key1 in new_dict1:
        basic_dict1[key1] = new_dict1[key1]
    return basic_dict1

basic_dict1 ={'a': 1,'b': 2, 'c':3}
new_dict1 = {'a': 4, 'd':5, 'e':6}
print(update_dict1(basic_dict1,new_dict1))

9.實(shí)現(xiàn)屬于自己的items方法:將字典轉(zhuǎn)換成列表,字典中的鍵值對(duì)轉(zhuǎn)換成元祖。(不能使用items方法)yt_items(字典)例如:{'a': 1, 'b': 2, 'c': 3} - --> [('a', 1), ('b', 2), ('c', 3)]
def chang_tuple_list(dict1):
    list1 = []
    for key1 in dict1:
        tuple1 = (key1, dict1[key1])
        print(tuple1)
        list1.append(tuple1)
    return list1
  dict1 = {'a': 1, 'b': 2, 'c': 3}
  print(chang_tuple_list(dict1))

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 多數(shù)人為了逃避真正的思考愿意做任何事情。 ——美團(tuán)網(wǎng)創(chuàng)始人兼CEO王興 這段時(shí)間一直在想,如何提高自己的思考能力?...
    Rh向日葵閱讀 724評(píng)論 0 0
  • 《批判性思維》 R:另外一個(gè)扭曲的認(rèn)知是所謂的從眾效應(yīng),它指的是人在下意識(shí)讓自己的想法向大多數(shù)人的想法靠攏的傾向。...
    Nealzeng閱讀 275評(píng)論 0 0
  • 死性不改的第四個(gè)晚自習(xí)
    吾道唯孤閱讀 101評(píng)論 0 0

友情鏈接更多精彩內(nèi)容