zoukankan      html  css  js  c++  java
  • 常用模块

    一.random  随机模块    

      from xxx import xxx     从xxx里面导入xxx

    import random
    
    print(random.randint(10,20)) # 随机整数
    print(random.random())  # 0-1 之内随机小数
    print(random.uniform(10,20)) # 随机小数
    
    lst = ["1","2","3"]
    random.shuffle(lst)   # 随机打乱顺序
    print(lst)
    
    print(random.choice(lst))  # 随机选一个
    
    print(random.sample(lst,2))  # 随机选几个
    

    二.Counter 计数模块

    from collections import Counter
    
    print(Counter("fsfsa62fes"))  # 计数   返回一个字典
    d = Counter("fsfsa62fes")
    for k,v in d.items():
        print(k,v)
    

    三.defaultdict 默认值字典模块      

    from collections import defaultdict
    
    dd = defaultdict(lambda :"666")  # 创建空字典 每个v的值都是666
    
    print(dd["你"])
    print(dd)
    
    from collections import OrderedDict # 3.5之前用
    
    dic = OrderedDict()  # 创建一个有序字典

    四.namedtuple 命名元组

      结构化时间就是用的这个

    from collections import namedtuple
    
    Person = namedtuple("Person",["name","age"])  # 类似于类 不能写方法
    
    p1 = Person("卡沙","18")
    
    print(p1.name,p1.age)

    五.栈,队列

      栈 : 先进后出

      队列 : 先进先出

    # 栈   先进后出
    
    class StackFullException(Exception):
        pass
    
    class StackEmptyException(Exception):
        pass
    
    class Stack:
    
        def __init__(self,size):
            self.size = size
            self.lst = []
            self.top = 0                    # 指针
    
        def push(self,el):                    # 入栈
            if self.top >= self.size:     # 指针 大于等于 容量的时候 溢出
                raise StackFullException
            else:
                self.lst.insert(self.top,el)   #放元素
                self.top += 1              # 放完指针向上移动,指向空
    
        def pop(self):                         # 出栈
            if self.top == 0:          # 指针指在0的时候没有数据
                raise StackEmptyException
            else:
                self.top -= 1          # 向下移动 指向有东西的位置
                el = self.lst.pop(self.top) # pop
                return el
    
    s = Stack(2)
    s.push("1")
    s.push("2")
    # s.push("3")
    # s.push("4")
    
    print(s.pop())
    print(s.pop())
    # print(s.pop())
    
    import queue
    
    q = queue.Queue()     # 创建一个队列
    
    q.put("1")     # 用 put 添加
    q.put("2")
    q.put("3")
    q.put("4")
    
    print(q.get())    # 拿
    print(q.get())
    print(q.get())
    print(q.get())
    
    from collections import deque    # 双向队列
    
    d = deque()     # 创建双向队列
    d.append("1")  # 默认是右侧添加
    d.append("2")
    d.append("3")
    d.appendleft("4")   # left 在左侧添加
    d.appendleft("5")
    
    print(d.pop())            # 默认从右边拿数据
    print(d.pop())
    print(d.pop())
    print(d.popleft())        # 从左边拿数据
    print(d.popleft())

     六.time时间模块

      时间戳格式化 : 先把时间戳 转换成结构化时间(localtime),再格式化成想要的格式(strftime)

      时间转换成时间戳 : 先把字符串时间转换成结构化时间(strptime),再转换成时间戳(mktime)

    import time
    
    a = 1559302530
    s = time.localtime(a)      #把时间戳 转换成结构化时间
    t = time.strftime("%Y-%m-%d %H:%M:%S",s)    # 格式化时间
    print(t)
    
    
    s = "2018-01-01 22:22:22"
    t = time.strptime(s,"%Y-%m-%d %H:%M:%S")  # 把字符串转化成结构化时间
    print(time.mktime(t)) # 转换成时间戳  和localtime配对
    

    七.functools 函数工具模块

    from functools import wraps  # 可以改变一个函数的名字
    
    def wrapper(fn):
        @wraps(fn)
        def inner(*args,**kwargs):
            print("qian")
            ret = fn(*args,**kwargs)
            print("hou")
            return ret
        return inner
    
    @wrapper
    def fu():
        print(666)
        return 666
    
    print(fu)
    
    from functools import reduce
    # reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
    #     ((((1+2)+3)+4)+5)
    ret = reduce(lambda x,y:x + y ,[1,3,4,5],2) #
    print(ret)
    
    from functools import partial # 固定函数中的某些参数值
    
    def chi(zs,fs):
        print(zs,fs)
    
    
    chi2 = partial(chi,fs="ji")  # 不改原函数的基础上设置默认值参数
    chi2("大庙反")
    chi2("大庙反")
    chi2("大庙反")
    chi2("大庙反")
    

      

  • 相关阅读:
    git中Please enter a commit message to explain why this merge is necessary.
    用$(this)选择其下带有class的子元素
    将某页面中ajax中获取到的信息放置到sessionStorage中保存,并在其他页面调用这些数据。
    返回顶部黑科技
    对于div里面内容过大根据长度或者宽度进行适配,然后可以滚轮缩放的功能
    vue runtime报错问题
    webpack简单配置
    input type=color 设置颜色
    vue统一注册组件
    vue模板字符串写法
  • 原文地址:https://www.cnblogs.com/q767498226/p/10182545.html
Copyright © 2011-2022 走看看