- 写一个函数将一个指定的列表中的元素逆序( 如[1, 2, 3] -> [3, 2, 1])(注意:不要使 表自带的逆序函数)
def reversed_order(list1:list):
new_list = []
i = -1
for x in list1:
new_list.insert(i, x)
i -= 1
return new_list
- 写一个函数,提取出字符串中所有奇数位上的字符
# 此题的奇数位,该处理解为字符的奇数位而非字符下标的奇数位
def odd_str(s: str):
list1 = []
for x in range(len(s)):
if x % 2 == 0:
list1.append(s[x])
return list1
- 写一个匿名函数,判断指定的年是否是闰年
#输入的年是闰年的时候就返回True,不是闰年的时候就返回False
year = lambda x: x % 400 == 0 or (x % 100 != 0 and x % 4 == 0)
- 写函数,提去字符串中所有的数字字符。
例如: 传入'ahjs233k23sss4k' 返回: '233234'
def extract_num(s):
y =''
for x in range(len(s)):
if '0' <= s[x] <= '9':
y += s[x]
return y
- 写一个函数,获取列表中的成绩的平均值,和最高分
def average_max(list1:list):
sum = 0
max = list1[0]
for i in list1:
sum += i
if max < i:
max = i
return sum / (len(list1)), max
- 写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新的列表返回给调用者
def odd_subscript(list1):
list2 = []
for i in range(len(list1)):
if i % 2 != 0:
list2.append(list1[i])
return list2
- 实现属于自己的字典update方法:用一个字典去更新另一个字典的元素(不能使用自带的update方法)
yt_update(字典1, 字典2)
def update_dict(update_d:dict,d2:dict):
for x in update_d:
d2[x] = update_d[x]
return d2
- 实现属于自己的items方法:将字典转换成列表,字典中的键值对转换成元祖。(不能使用items方法)
yt_items(字典)
例如:{'a': 1, 'b':2, 'c':3} ---> [('a', 1), ('b', 2), ('c', 3)]
def dict_list(d1: dict):
list1 = []
for x in d1:
y = (x, d1[x])
list1.append(y)
return list1
- 有一个列表中保存的所一个班的学生信息,使用max函数获取列表中成绩最好的学生信息和年龄最大的学生信息
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}
]
注意: max和min函数也有参数key
print(max(all_student,key=lambda x:x['score']))
print(max(all_student,key=lambda x:x['name']))
网友评论