2019-01-05day10作业

2019-01-05  本文已影响0人  Little茂茂
  1. 写一个函数将一个指定的列表中的元素逆序( 如[1, 2, 3] -> [3, 2, 1])(注意:不要使用自带的逆序函数)
def nixu(list1):
    """
    将列表内元素逆序
    :param list1:指定的列表
    :return:逆序列表
    """
    list2 = []
    for index in list1[::-1]:
        list2.append(index)
    return list2

  1. 写一个函数,提取出字符串中所有奇数位上的字符
def jishu(str1):
    """
    提取出所有奇数位上的字符
    :param str1:
    :return:str2
    """
    str2 = ''
    for index in str1[::2]:
        str2 += index
    return str2
  1. 写一个匿名函数,判断指定的年是否是闰
func1=lambda x:x % 4 == 0 or x % 100 ==0 and x % 400 != 0
  1. 写函数,提去字符串中所有的数字字符。
    例如: 传入'ahjs233k23sss4k' 返回: '233234'
def nums(str1):
    """
    提取字符串中所有数字字符
    :param str1:指定字符串
    :return:所有数字字符组成的字符串
    """
    str2 = ''
    for index in str1:
        if '0' <= index <= '9':
            str2 += index
    return str2
  1. 写一个函数,获取列表中的成绩的平均值,和最高分
def fen(list1):
    """
    求列表中数字的平均值和最大值
    :param list1:指定列表
    :return:平均值和最大值
    """
    sum = 0
    count = 0
    for index in list1:
        sum += index
        count += 1
        avg = sum/count
    for index in list1:
    return avg, max(list1)
  1. 写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新的列表返回给调用者
def jishuwei(n):
    """
    将指定列表或元祖的奇数位数字提取组成新的列表或元祖返回
    :param n:指定列表或元祖
    :return:提取的奇数位组成的新列表或元祖
    """
    if type(n) is 'list':
        list2=[]
        for index in n[::2]:
            list2.append(index)
        n=list2
        return n
    else:
        list2=list(n)
        list3=[]
        for index in list2[::2]:
            list3.append(index)
        n=tuple(list2)
        return n
  1. 实现属于自己的字典update方法:用一个字典去更新另一个字典的元素(不能使用自带的update方法)
    yt_update(字典1, 字典2)
def update(dict1,dict2):
    """
    用一个字典去更新另一个字典
    :param dict1:给别的字典更新的字典
    :param dict2:被更新的字典
    :return:合并后的新字典
    """
    list1=list(dict2.keys())
    list2=list(dict2.values())
    for index in range(len(list2)):
        dict1[list1[index]]=list2[index]
        return dict1

  1. 实现属于自己的items方法:将字典转换成列表,字典中的键值对转换成元祖。(不能使用items方法)
    yt_items(字典)
    例如:{'a': 1, 'b':2, 'c':3} ---> [('a', 1), ('b', 2), ('c', 3)]
def zhuanghuan(dict1):
    list1=[]
    for key in dict1:
        tuple1=key,dict1[key]
        list1.append(tuple1)
    return  list1
  1. 有一个列表中保存的所一个班的学生信息,使用
    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

def chengji(list1):
    """
    获取列表中成绩最好和年龄最大的学生信息
    :param list1:班级学生信息组成的列表
    :return:max1成绩最好学生的信息max2年龄最大学生的信息
    """
    list2 = []
    list3 = []
    for i in all_student:
        list2.append(i['score'])
    for i in all_student:
        if i['score'] == max(list2):
            max1 = i
    for j in all_student:
        list3.append(j['age'])
    for j in all_student:
        if j['age'] == max(list3):
            max2 = j
    return max1, max2

上一篇下一篇

猜你喜欢

热点阅读