美文网首页
day10、作业 2019-01-05

day10、作业 2019-01-05

作者: 绝世小丑 | 来源:发表于2019-01-05 21:44 被阅读0次

1. 写一个函数将一个指定的列表中的元素逆序( 如[1, 2, 3] -> [3, 2, 1])(注意:不要使用表自带的逆序函数)

def ni_xu(new_list):
    """将一个指定的列表中的元素逆序"""
    print('原列表为:', new_list)
    i = 0
    the_list = []
    while i < len(new_list):
        j = len(new_list)
        chr1 = new_list[j - 1 - i]
        the_list.append(chr1)
        i += 1
    new_list = the_list
    print('逆序后的列表为:')
    return new_list


list1 = [1, 2, 3, 4, 5]
print(ni_xu(list1))

运行结果:

原列表为: [1, 2, 3, 4, 5]
逆序后的列表为:
[5, 4, 3, 2, 1]

2. 写一个函数,提取出字符串中所有奇数位上的字符

def tqjszf(new_list):
    """提取出字符串中所有奇数位上的字符"""
    print('原列表为:', new_list)
    i = 0
    the_list = []
    while i < len(new_list):
        if i & 1 == 1:
            chr1 = new_list[i]
            the_list.append(chr1)
        i += 1
    new_list = the_list
    print('字符串中所有奇数位上的字符有:')
    return new_list


list1 = [1, 2, 3, 4, 5]
print(tqjszf(list1))

运行结果:

原列表为: [1, 2, 3, 4, 5]
字符串中所有奇数位上的字符有:
[2, 4]

3. 写一个匿名函数,判断指定的年是否是闰年

year = lambda num1: num1 % 400 == 0 or (num1 % 4 == 0 and num1 % 100 != 0)
print('是否为闰年?', year(2016))
print('是否为闰年?', year(2019))

运行结果:

是否为闰年? True
是否为闰年? False

4. 写函数,提取字符串中所有的数字字符。

例如: 传入'ahjs233k23sss4k' 返回: '233234'

def yes_num(strs):
    """提取字符串中所有的数字字符"""
    str1 = ''
    for i in strs:
        if '0' <= i <= '9':
            str1 += i
    return str1


str1 = 'adc123zxc456'
print(yes_num(str1))

运行结果:

123456

5. 写一个函数,获取列表中的成绩的平均值,和最高分

def mean_max(new_list: list):
    """获取列表中的成绩的平均值,和最高分"""
    mean1 = 0
    num1 = new_list[0]
    max1 = 0
    i = 0
    while i < len(new_list):
        mean1 += new_list[i]
        num1 = new_list[i]
        if max1 < num1:
            max1 = num1
        i += 1
    mean2 = mean1 / len(new_list)
    print('列表中的成绩的平均值为:', mean2, '\n列表中的成绩的最高分为:', max1)
    return mean2, max1


list1 = [80, 90, 70, 60, 100]
mean_max(list1)

运行结果:

列表中的成绩的平均值为: 80.0 
列表中的成绩的最高分为: 100

6. 写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新的列表返回给调用者

def jsys(od_sq):
    """返回所有奇数位索引对应的元素"""
    new_list = []
    i = 0
    while i < len(od_sq):
        if i + 1 & 1 == 0:
            new_list.append(od_sq[i])
        i += 1
    return new_list


list1 = [0, 1, 2, 3, 4, 5]
tuple1 = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
print(jsys(list1))
print(jsys(tuple1))

运行结果:

[1, 3, 5]
[1, 3, 5, 7, 9]

7. 实现属于自己的字典update方法:用一个字典去更新另一个字典的元素(不能使用自带的update方法)

yt_update(字典1, 字典2)



def update_plus(dict1, dict2):
    for key in dict2:
        dict1[key] = dict2[key]


def update_dict_plus(new_dict, other_dict: dict):
    """用一个字典去更新另一个字典的元素"""
    list1 = list(new_dict)
    list1.append(new_dict[list1[0]])
    print('更新前字典为:', other_dict)
    print('要更新的键值对为:', new_dict)
    other_dict[str(list1[0])] = str(list1[1])
    print('更新后的字典为:', other_dict)
    return other_dict


dict1 = {'name': '阿黄'}
dict2 = {'sno': '1', 'name': '张三', 'age': '3'}
update_dict_plus(dict1, dict2)
print(dict2)

运行结果:

更新前字典为: {'sno': '1', 'name': '张三', 'age': '3'}
要更新的键值对为: {'name': '阿黄'}
更新后的字典为: {'sno': '1', 'name': '阿黄', 'age': '3'}
{'sno': '1', 'name': '阿黄', 'age': '3'}

8. 实现属于自己的items方法:将字典转换成列表,字典中的键值对转换成元祖。(不能使用items方法)

yt_items(字典)
例如:{'a': 1, 'b':2, 'c':3} ---> [('a', 1), ('b', 2), ('c', 3)]

def chang_plus(new_dict: dict):
    """将字典转换成列表"""
    list1 = []
    new_list = []
    for i in new_dict:
        list1 = []
        list1.append(i)
        list1.append(new_dict[i])
        new_tuple = tuple(list1)
        new_list.append(new_tuple)
    return new_list


dict1 = {'a': 1, 'd': 2, 'c': 3}
list1 = chang_plus(dict1)
print(list1)

运行结果:

[('a', 1), ('d', 2), ('c', 3)]

9. 有一个列表中保存的所一个班的学生信息,使用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 score_age(new_list):
    """获取列表中成绩最好的学生信息和年龄最大的学生信息"""
    best_score = 0
    max_age = 0
    i = 0
    while i < len(new_list):
        dict1 = new_list[i]
        if best_score < dict1['score']:
            best_score = dict1['score']
            best_stu = dict1
        i += 1
    print('列表中成绩最好的学生信息为:', best_stu)
    j = 0
    while j < len(new_list):
        dict2 = new_list[j]
        if max_age < dict2['age']:
            max_age = dict2["age"]
            max_stu = dict2
        j += 1
    print('列表中年龄最大的学生信息为:', max_stu)




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}
]
score_age(all_student)

运行结果:

列表中成绩最好的学生信息为: {'name': 'stu22', 'age': 29, 'score': 99}
列表中年龄最大的学生信息为: {'name': 'stu1', 'age': 30, 'score': 79}

相关文章

网友评论

      本文标题:day10、作业 2019-01-05

      本文链接:https://www.haomeiwen.com/subject/xrkdrqtx.html