码迷,mamicode.com
首页 > 其他好文 > 详细

常用模块

时间:2018-12-27 03:34:20      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:字符   bsp   UNC   lse   dom   pass   结构化   too   highlight   

一.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")  # 创建空字典 每个k的值都是666

print(dd["你"])
print(dd)

from collections import OrderedDict # 3.5之前用

dic = OrderedDict()  # 创建一个有序字典

四.栈,队列

  栈 : 先进后出

  队列 : 先进先出

# 栈   先进后出

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("大庙反")

  

 

常用模块

标签:字符   bsp   UNC   lse   dom   pass   结构化   too   highlight   

原文地址:https://www.cnblogs.com/q767498226/p/10182545.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!