10.11 day10作业

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')]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 如果不注意,大概很多人认为“闰月”与“闰年”是一个意思,其实不是,虽说只是一字之差,所包含的意思却相差很远。 “闰...
    雨落未惊风阅读 8,608评论 1 2
  • 在C语言中,五种基本数据类型存储空间长度的排列顺序是: A)char B)char=int<=float C)ch...
    夏天再来阅读 3,421评论 0 2
  • 一. Flume安装与配置 Flume官网: http://flume.apache.org JDK版本要求1.7...
    GuangHui阅读 388评论 0 0
  • 人与动物的区别是能否安排自己的生活,动物的生活完全依赖于环境和基因,虽然一个人的成长过程也会受到自己遭遇的环境包括...
    如一书阅读 292评论 0 4