01-06day10作业

1. 写一个函数将一个指定的列表中的元素逆序( 如[1, 2, 3] -> [3, 2, 1])(注意:不要使 表自带的逆序函数)
def nix(num):
    cis = 0
    if len(num) % 2 == 1:
        while (len(num)-1)/2 >= cis >= 0:
            num[cis],num[len(num) - cis - 1] = num[len(num) - cis - 1], num[cis]
            cis += 1
    else:
        while len(num)/2 >= cis >= 0:
            num[cis],num[len(num) - cis - 1] = num[len(num) - cis - 1], num[cis]
            cis += 1
    return num
print(nix([2,4,5,7,9]))
for x in range(len(list1)//2):
写一个函数,提取出字符串中所有奇数位上的字符
def jis(num):
    str1 = ''
    for x in range(1,len(num),2):
        str1 += num[x]
    return str1
print(jis('1234567'))
写一个匿名函数,判断指定的年是否是闰年

当公历年份不是一百的倍数时,需要被4整除,如2004÷4=501,所以是闰年;当公里年份是100的倍数时,必须是400的倍数才行

runnian = lambda x:(x % 4 == 0 and x % 100 != 0) or (x % 400 == 0 and x % 100 == 0)
num = int(input('请输入一个年份:'))
print('%d是否是闰年:' % num ,runnian(num))
写函数,提去字符串中所有的数字字符。

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

def nums1(nums):
    srt2 = ''
    for x in range(len(nums)):
        if '0' <= nums[x] <= '9':
            srt2 += nums[x]
    return srt2
print(nums1('ahjs233k23sss4k'))
写一个函数,获取列表中的成绩的平均值,和最高分
def fens(list2):
    max1 = 0
    pj = 0
    for x in range(len(list2)):
        if max1 < list2[x]['成绩']:
            max1 = list2[x]['成绩']
        elif max1 > list2[x]['成绩']:
            max1 = max1
        pj += list2[x]['成绩']
    pj = pj // len(list2)
    return  max1, pj
print(fens([
    {'name':'张小','年龄':19,'成绩':66},
    {'name':'枯叶','年龄':22,'成绩':88},
    {'name':'城西','年龄':17,'成绩':66}
]))
写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新的列表返回给调用者
def jis(nums):
    news_nums = []
    for x in range(1,len(nums),2):
        news_nums.append(nums[x])
    return news_nums
print(jis([1,2,3,4,5,6,7]))
实现属于自己的字典update方法:用一个字典去更新另一个字典的元素(不能使用自带的update方法)yt_update(字典1, 字典2)
def yt_update(dict1):
    for y in dict1[1]:
        if dict1[1][y] in dict1[0]:
            dict1[0][y] = dict1[1][y]
        elif dict1[1][y] not in dict1[0]:
            dict1[0][y] = dict1[1][y]
    return  dict1[0]
print(yt_update([
    {'name': '张小', '年龄': 19, '成绩': 66},
    {'喜欢的': '枯叶', '年龄': 22, '成绩': 88},
]))
实现属于自己的items方法:将字典转换成列表,字典中的键值对转换成元祖。(不能使用items方法) yt_items(字典)

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

def items2(nums):
    list1 = []
    for x in nums:
        list1.append((x,nums[x]))
    return list1
print(items2({'a': 1, 'b':2, 'c':3}))
有一个列表中保存的所一个班的学生信息,使用max函数获取列表中成绩最好的学生信息和年龄最大的学生信息

注意: max和min函数也有参数key

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}
]
a = max(all_student,key=lambda x:x['score'])
print(a)
b = min(all_student,key=lambda x:x['age'])
print(b)

你可能感兴趣的:(01-06day10作业)