美文网首页
time,random模块

time,random模块

作者: 全宇宙最帅De男人 | 来源:发表于2018-02-10 19:49 被阅读0次

    time模块

    1.time.time() 时间戳
    2.time.sleep(3) 程序在此处停留3s
    3.time.clock() 计算CPU执行时间
    4.time.gmtime() utc时间,struct_time结构化时间:(tm_year=* ,...)
    5.time.localtime() 本地时间
    6.time.strftime(format,p_tuple)

        %Y : year
        %m : month
        %d : day
        %H : hour
        %M : minute
        %S : second
    

    7.time.strptime(string,format) 结构化时间
    8.time.ctime() 根据时间戳显示时间,时间格式固定
    9.time.mktime() 根据时间转换成时间戳
    ** datetime模块 **
    datetime.datetime.now() 正常格式的时间

    random模块

    1.random.random() 0-1之间随机数
    2.random.randint(1,8) 1-8之间随机数,包括8
    3.random.choice(序列) 序列中随机元素
    4.random.sample(序列,int) 序列中随机选择int个元素
    5.random.randrange(1,8) 1-8随机,不包括8
    6.产生5位字母数字组成的随机验证码

        import random
        def v_code():
            code=''
            for i in range(5):
    
                add = random.choice([random.randrange(10),chr(random.randrange(65,91))])
    
                code+=str(add)
            print(code)
    
        v_code()
    

    算法一:字符串反转

        #字符串切片,格式[start:end:step]
        def func1(s):
            return ''.join(s[::-1])
        #递归
        def func2(s):
            if len(s) < 1:
                return s
            return func2(s[1:])+s[0]
        #使用栈
        def func3(s):
            l=list(s)
            result=''
            while len(l)>0:
                result+=l.pop()
            return result
        #for循环-->enumerate()枚举,可以返回可迭代对象中每个元素和对应的索引
        def func4(s):
            result=''
            max_index=len(s)-1
            for index,value in enumerate(s):
                result += s[max_index-index]
            return result
        #使用reduce
        from functools import reduce
        def func5(s):
            result=reduce(lambda x,y:y+x,s)
            return result
    

    留给大家一个问题(答案后台留言):

        def func6(s):
            lst=list(s)
            result=''.join(lst.reverse())
            return result
        print(func6('hello world!'))
    

    1.运行结果是什么?
    2.怎么改进?
    提示:lst.reverse()返回NoneType
    运行结果是什么

    相关文章

      网友评论

          本文标题:time,random模块

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