Day-10 - 作業(yè)(2018-10-11)

寫一個函數(shù)將一個指定的列表中的元素逆序(如[1, 2, 3] -> [3, 2, 1])(注意:不要使 表自帶的逆序函數(shù))

def reverse_list(any_list: list):
    """
    聲明一個函數(shù),使指定列表的元素反序
    :param any_list:  指定一個列表
    :return: 指定的列表
    """
    for index in range(len(any_list)):
        # 不停地將最后一個元素彈出,并將其插入到index下標中
        last_item = any_list.pop(-1)
        any_list.insert(index, last_item)


list1 = [1, 2, 3, 4, 5, 6, 7]
reverse_list(list1)
print(list1)

寫一個函數(shù),提取出字符串中所有奇數(shù)位上的字符

def yxy_odd(any_str: str):
    """
    聲明一個函數(shù),將指定字符串奇數(shù)位上的字符提取出來
    :param any_str: 字符串
    :return: 列表
    """
    list1 = []
    for index in range(len(any_str)):
        if not index & 1:
            list1.append(any_str[index])

    return list1


str1 = 'abcdefg'
str2 = yxy_odd(str1)
print(str2)  # ['a', 'c', 'e', 'g']

寫一個匿名函數(shù),判斷指定的年是否是閏

re = lambda year: (year % 4 == 0 and year % 100 !=0) or year % 400 == 0
print(re(2000))
print(re(1990))
print(re(1992))

使用遞歸打印:

n = 3
的時候


    @
  @ @ @
@ @ @ @ @


n = 4
的時候:


      @
    @ @ @
  @ @ @ @ @
@ @ @ @ @ @ @


def print_at(n: int, i: int):
    """
    定義一個函數(shù),打印三角形
    :param n: 數(shù)字,整數(shù)
    :param i: 數(shù)字,i=n
    :return:
    """
    if n == 1:
        print('@'.center(2*i-1,' '))
        return
    print_at(n-1, i)
    print(('@'*(2*n - 1)).center(2*i-1,' '))

print_at(4,4)

寫函數(shù), 利用遞歸獲取斐波那契數(shù)列中的第10個數(shù),并將該值返回給調(diào)用者。

def fb_num(n):
    if n <= 2:
        return 1
    return fb_num(n-1) + fb_num(n-2)


num = fb_num(10)
print(num)  # 55

寫一個函數(shù),獲取列表中的成績的平均值,和最高分

def yxy_score(scores: list) -> tuple:
    """
    聲明一個函數(shù),用來計算列表中成績的平均值和最高分,返回一個元組
    :param scores: 成績,列表
    :return: 元組,第一個值為平均值,第二個值為最高分
    """
    average = sum(scores)/len(scores)
    max_num = max(scores)
    return average, max_num


list1 = [90, 89, 98, 100, 95]
score = yxy_score(list1)
print('平均分:%s, 最高分: %s' % (score[0],score[1]))  # 平均分:94.4, 最高分: 100

寫函數(shù),檢查獲取傳入列表或元組對象的所有奇數(shù)位索引對應(yīng)的元素,并將其作為新的列表返回給調(diào)用者

def odd_item(items: list or tuple) -> list:
    """
    聲明一個函數(shù),獲取列表或者元組奇數(shù)位索引的元素,并返回一個新列表
    :param items: list or tuple
    :return:
    """
    list1 = []
    for item in items[::2]:
        list1.append(item)
    return list1


list1 = [1, 2, 3, 4, 5, 6]
tuple1 = ('a', 'b', 'c', 'd', 'e', 'f')
print(odd_item(list1))  # [1, 3, 5]
print(odd_item(tuple1))  # ['a', 'c', 'e']

實現(xiàn)屬于自己的字典update方法:用一個字典去更新另一個字典的元素(不能使用自帶的update方法)
yt_update(字典1, 字典2)

def yxy_update(original_dict: dict, update_dict: dict) -> dict:
    """
    聲明一個函數(shù),用update_dict字典中的元素更新original_dict字典,并返回更新結(jié)果
    :param original_dict: 字典
    :param update_dict: 字典
    :return: 字典
    """
    for new_key in update_dict:
        original_dict[new_key] = update_dict[new_key]

    return original_dict


dict1 = {'a':1 , 'b': 2, 'c': 3, 'd': 4}
dict2 = {'f': 200, 'g': 300, 'b': 400, 'h': 500}
new_dict = yxy_update(dict1, dict2)
print(new_dict)  # {'a': 1, 'b': 400, 'c': 3, 'd': 4, 'f': 200, 'g': 300, 'h': 500}

實現(xiàn)屬于自己的items方法:將字典轉(zhuǎn)換成列表,字典中的鍵值對轉(zhuǎn)換成元祖。(不能使用items方法)
yt_items(字典)
例如:{'a': 1, 'b': 2, 'c': 3} - --> [('a', 1), ('b', 2), ('c', 3)]

def yxy_items(dict1: dict) -> list:
    """
    聲明一個函數(shù),將字典轉(zhuǎn)換成列表,字典中的鍵值對轉(zhuǎn)換成元組
    :param dict1: 字典
    :return: 列表,元素為元組
    """
    list1 = []
    for key in dict1:
        # 元組不能添加,只能賦值
        tuple1 = (key, dict1[key])
        list1.append(tuple1)

    return list1


dict1 = {'a': 1, 'b': 2, 'c': 3}
list1 = yxy_items(dict1)
print(list1)  # [('a', 1), ('b', 2), ('c', 3)]
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

  • 一、快捷鍵 ctr+b 執(zhí)行ctr+/ 單行注釋ctr+c ...
    o_8319閱讀 6,032評論 2 16
  • 5Python集合容器 數(shù)據(jù)結(jié)構(gòu)數(shù)據(jù)結(jié)構(gòu) 一般將數(shù)據(jù)結(jié)構(gòu)分為兩大類: 線性數(shù)據(jù)結(jié)構(gòu)和非線性數(shù)據(jù)結(jié)構(gòu)。 線性數(shù)據(jù)結(jié)構(gòu)...
    清清子衿木子水心閱讀 1,718評論 0 1
  • 文/臨溪為硯 1. 前不久,我因公出差,途徑香港,一位定居于此的老友,邀請我去她的店里做客。 她的店鋪位于香港中環(huán)...
    臨溪為硯閱讀 2,015評論 13 45

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