美文网首页
10.11 day10作业

10.11 day10作业

作者: 烟雨江畔丶 | 来源:发表于2018-10-11 19:51 被阅读0次

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

def chang_list(list=list):
    count = 0
    if count != len(list):
        for item in list[:]:
            list.insert(0,item)
            count  += 1
    list = list[:count]
    return list
print(chang_list([1,2,3]))
# [3, 2, 1]
  1. 写一个函数,提取出字符串中所有奇数位上的字符
def str_get(n=str):
    str2 = ''
    for item in range(len(n)):
        if item%2 != 0:
            str2 += n[item]
    return str2
print(str_get("abcde,'a',123456"))
# bd,a,246
  1. 写一个匿名函数,判断指定的年是否是闰
new_year = lambda number:number%4 == 0 and number%100 != 0
print(new_year(1204))     #  True
print(new_year(1200))    # False

  1. 使用递归打印:
 n = 3
 的时候

      @
    @ @ @
  @ @ @ @ @

 n = 4
 的时候:
       @
     @ @ @
   @ @ @ @ @
 @ @ @ @ @ @ @

写函数, 利用递归获取斐波那契数列中的第10个数,并将该值返回给调用者。

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

list1 = [100, 23, 80, 49, 78, 12, 60]
def mean_max(list=list):
    maxnum = 0
    for num in list:
        if num > maxnum:
            maxnum = num
        mean = sum(list)/len(list)
    return mean,maxnum
print(mean_max([100, 23, 80, 49, 78, 12, 60]))
# (57.42857142857143, 100)

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

def new_list_dict(list):
    newlist = []
    for x in list[::2]:
        newlist.append(x)
    return newlist

print(new_list_dict([1,2,3,4,5]))
print(new_list_dict((123,231,343,'asd')))
[1, 3, 5]
[123, 343]

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

def update(dict1:{},dict2:{}):
    list1 = []
    list2 = []
    count = 0
    for x in dict2:
        list1.append(x)
        list2.append(dict2[x])
        dict1[x] = list2[count]
        count += 1
    return dict1
a1 = {1:'abd',2:'ccc',3:'dasd',4:'dasdsa'}
a2 = {5:'asdas1',6:'dasdsa',7:'32123',4:'2312',3:'23asdsa'}
d3 = update(a1,a2)
print(d3)
{1: 'abd', 2: 'ccc', 3: '23asdsa', 4: '2312', 5: 'asdas1', 6: 'dasdsa', 7: '32123'}

实现属于自己的items方法:将字典转换成列表,字典中的键值对转换成元祖。(不能使用items方法) yt_items(字典)
例如:{'a': 1, 'b': 2, 'c': 3} - --> [('a', 1), ('b', 2), ('c', 3)]

def items(dict:{}):
    list = []
    tuple = ()
    for item in dict:
        tuple = (item,dict[item])
        list.append(tuple)
    return list

a1 = {'a':1,'s':'312',2:'dasd','sda':'dasd'}
b1 = items(a1)
print(b1)
[('a', 1), ('s', '312'), (2, 'dasd'), ('sda', 'dasd')]

相关文章

网友评论

      本文标题:10.11 day10作业

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