寫一個函數(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ù)位索引對應的元素糊饱,并將其作為新的列表返回給調(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字典狭归,并返回更新結果
: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方法:將字典轉換成列表夭坪,字典中的鍵值對轉換成元祖。(不能使用items方法)
yt_items(字典)
例如:{'a': 1, 'b': 2, 'c': 3} - --> [('a', 1), ('b', 2), ('c', 3)]
def yxy_items(dict1: dict) -> list:
"""
聲明一個函數(shù)唉铜,將字典轉換成列表台舱,字典中的鍵值對轉換成元組
: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)]