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

    ---恢复内容开始---

    1.collections

    # 1.Counter(计数器)
    from collections import Counter
    s="alex like pig"
    print(Counter(s))

    # 2.deque 双向队列
    # 1.栈: FILO First in Last Out 先进后出
    # 2.队列: FIFO First in First Out 先进先出

    class StackEmptyError(Exception):
        pass
    class
    StackFullError(Exception):
        pass
    class
    Stack:
        def __init__(self,size):
            self.index=0
            self.size=size
            self.lst=[]
        def pop(self):
            if self.index>0:
                self.index-=1
                ret=self.lst[self.index]
                return ret
            else:
                raise StackEmptyError("stack has already empty")
        def push(self,el):
            if self.index>self.size:
                raise StackFullError("stack is full")
            else:
                self.lst.insert(self.index,el)
                self.index+=1
        def clear(self):
            self.lst.clear()
            self.index=0
    # queue 队列
    import queue
    q=queue.Queue()
    q.put("李嘉诚"#添加
    q.put("张开")
    print(q)
    print(q.get())
    q.get()

    # 双向队列
    from collections import deque
    q=deque()
    q.append("张开")
    q.appendleft("马化腾"#左侧添加
    q.pop()
    q.popleft() #左侧删除

    # 3.namedtuple 命名元祖
    from collections import namedtuple
    nt=namedtuple("point",["x","y"])
    #相当于创建了一个point的类
    p=nt(1,2)
    print(p)
    print(p.x)
    print(p.y)

    class Point:
        def __init__(self,x,y):
            self.x=x
            self.y=y
    p=Point(1,2)
    print(p.x)
    print(p.y)

    # defaultdict  给字典设置默认值
    from collections import defaultdict
    dd=defaultdict(list)
    dd["娃哈哈"].append(56)
    dd["娃哈"].append(57)
    print(dd)  #当key不存在的时候,会自动之执行构造方法中传递的内容

    lst = [11, 22, 33, 44, 55, 66, 77, 88, 99]
    dd = defaultdict(list)
    for el in lst:
        if el > 66:
            dd['key1'].append(el)
        else:
            dd['key2'].append(el)
    print(dd)

     2. time模块

            1. 时间戳. float 数字. 1970-01-01 00:00:00

            2. 格式化时间. %Y-%m-%d %H:%M:%S      %Y-%m-%d

            3. 结构化时间. 把时间拆分了。

            时间戳 -> 格式化时间

                f = 10086

                st = time.localtime(f)

                s = time.strftime("%Y-%m-%d %H:%M:%S", st)

            格式化时间 -> 时间戳

                s = "2018-01-01 12:58:46"

                st = time.strptime(s, "%Y-%m-%d %H:%M:%S")

                f = time.mktime(st)

            time.sleep()  睡眠多长时间

    3. random

            random.randint(start, end) [start, end]随机整数

            random.choice() 随机选择

            random.sample() 随机选择n个

     4. os和sys

            os 和操作系统相关

            sys  和解释器相关的

            sys.path.clear()

    ---恢复内容结束---

  • 相关阅读:
    (转)Quick Tip: Provisioning Web Parts to a Page(添加web part到page上,)
    解决sharepoint 2010浏览器在线浏览Word出错(非原创)
    转:部署带webpart的网页(Deploy a Page using a Feature)
    单点登录到sharepoint
    Feature Base On WebApp
    Create a New Document Using Office Web Apps
    学校合并与数组合并
    DNN拟合曲线
    使用tf.keras.layers.Layer自定义神经网络的层
    numpy
  • 原文地址:https://www.cnblogs.com/litieshuai/p/9761578.html
Copyright © 2011-2022 走看看