zoukankan      html  css  js  c++  java
  • day5:函数练习题

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

     1 #解1:
     2 def lis(x):
     3     lis_1 = []
     4     for i in range(len(x)):
     5         #循环下标
     6         if i % 2 == 1:
     7             lis_1.append(x[i])
     8     return lis_1
     9 test = lis([5,32,54,66,4])
    10 print(test)
    11 
    12 #解2:利用切片
    13 def lis(x):
    14     new_lis = x[1::2]#下标从1开始,【::】代表所有,2代表步长
    15     return  new_lis
    16 print(lis([21,423,65,76,3,2]))

    2、写函数,判断用户传入的对象、(字符串、列表、元祖)长度是否大于5.

    1 def test(x):
    2     if len(x) > 5:
    3         return True
    4 print(test([1,2,3]))

    3、写函数,检查传入列表的长度,如果大于2,那么仅保留前2个长度的内容,并将新内容返回给调用者

    def test(x):
        if len(x) > 2 :
            new_1 = x[0:2]
        return new_1
    print(test([2,3,5]))

    4、写函数、计算传入字符串中的【数字】、【字母】、【空格】及【其他】的个数

     1 def num(x):
     2     num_1 = 0
     3     str_1 = 0
     4     spac = 0
     5     other = 0
     6     for i in x:
     7        if i.isdecimal():
     8            num_1 += 1
     9        elif i.isspace() :
    10            spac += 1
    11        elif i.isalnum() :
    12            str_1 += 1
    13        else:
    14            other += 1
    15     return {'数字':num_1,'字母':str_1,'空格':spac,'其他':other}
    16 test = num('fjakla3224/  .2  &^@')
    17 print(test)
    View Code
  • 相关阅读:
    js获取url参数
    Ueditor百度编辑器中的 setContent()方法的使用
    js防止sql注入的参数过滤
    父级元素点击,遮盖了子元素的点击
    input onchange事件
    jq选择子元素
    nodejs mysql 执行多条sql语句
    java.util.ConcurrentModificationException
    Group by与having理解
    ibatis配置xml文件中CDATA的用法
  • 原文地址:https://www.cnblogs.com/sunjinchao/p/10714107.html
Copyright © 2011-2022 走看看