初识模块01

1. 简单了解模块
  写的每一个py文件都是一个模块.
  还有一些我们一直在使用的模块
  buildins 内置模块. print, input

  random 主要是和随机相关的内容
    random() 随机小数
    uninform(a,b) 随机小数

    randint(a,b) 随机整数

    choice() 随机选择一个
    sample() 随机选择多个

    shuffle() 打乱

2. Collections
  1. Counter 计数器

  2. namedtuple   命名元组->类似创建了一个类
  
  3. defaultdict 默认值字典
  4. OrderedDict 有序字典


  数据结构(队列, 栈(重点))

    栈:先进后出
      Stack

class Zhan:
    def __init__(self,saze):
        self.saze = saze
        self.lis = []
        self.top = 1

    def ru(self,a):
        if self.top > self.saze:
            raise Exception("栈溢出了")
        self.lis.insert(self.top,a)
        self.top += 1

    def chu(self):
        if self.top == 1 :
            raise Exception("没东西了")
        self.top -= 1
        self.lis.pop()

z = Zhan(3)
z.ru("卢本伟牛逼")
print(z.lis)
z.chu()
print(z.lis)
z.ru("卢本伟牛逼1")
z.ru("卢本伟牛逼2")
z.ru("卢本伟牛逼3")

print(z.lis)
z.chu()
print(z.lis)
z.chu()
print(z.lis)
z.chu()

  

    队列: 先进先出
    Queue

import queue
q = queue.Queue()
q.put("卢本伟")
q.put("PDD")
q.put("UU")

print(q.get())
print(q.get())
print(q.get())


# 创建双向队列
from collections import deque

d = deque() # 创建双向队列
d.append("卢本伟") #  在右侧添加
d.append("white")
d.append("55开")
d.append("张大仙")
d.appendleft("梦泪") # 在左边添加
d.appendleft("阿飞")
d.appendleft("恩宠")


print(d.pop()) # 从右边拿数据
print(d.pop()) # 从右边拿数据
print(d.pop()) # 从右边拿数据
print(d.pop()) # 从右边拿数据
print(d.popleft()) # 从左边拿数据
print(d.popleft()) # 从左边拿数据
print(d.popleft()) # 从左边拿数据

  

3. Time模块
  时间有三种:
  结构化时间 gmtime()    localtime()
  时间戳 time.time()       time.mktime()
  格式化时间 time.strftime()      time.strptime()

  时间转化:  通用时间格式"%Y-%m-%d %H:%M%S"           mktime 和 localtime 是东8时间


    数字 -> 字符串
    struct_time = time.localtime(数字)
    str = time.strftime("格式", struct_time)

import time           #导入时间模块
a = 354684569.9          #数据库存放的时间数据(时间戳)
t = time.localtime(a)     #把时间戳转化为python的结构化时间
b = time.strftime("%Y-%m-%d %H:%M:%S",t)       #把结构化时间转化为格式化时间(给人看的时间)
print(b)

  

    字符串 -> 数字
    struct_time = time.strptime(字符串, "格式")
    num = time.mktime(struct_time)

import time
s = input("请输入一个时间")           #输入一个字符串时间
t = time.strptime(s,"%Y-%m-%d %H:%M:%S")   #转化为结构化时间
a = time.mktime(t)     #结构化时间转化为数据库时间数据(时间戳)
print(a)

  

4. functools
  wraps 给装饰器中的inner改名字

def wrapper(fn):
    @wraps(fn)  # 把inner的名字改变成原来的func
    def inner(*args, **kwargs):
        print("前")
        ret = fn(*args, **kwargs)
        print("后")
        return ret

    return inner

@wrapper # func = wrapper(func)
def func():
    print('哈哈哈')

print(func.__name__) # func

  

  reduce 归纳.(与map()映射是对应着的)

# map 映射 reduce 归纳
print(list(map(lambda x: x**2, [i for i in range(10)])))

from functools import reduce
def func(a, b):
    return a * b 
# 会把我们每一个数据交给func去执行, 把默认值作为第一个参数传递给函数
# 第二个参数就是你这个序列中的第一个数据
# 接下来. 把刚才返回的结果作为第一个参数传递个a
# 继续吧刚才的结果给第一个参数. 把第三个数据传递给b
ret2 = reduce(func,[1,4,7,2,5,8,3,6,9])
ret1 = reduce(lambda x,y: x*y, [1,4,7,2,5,8,3,6,9])  #func = lambda x,y : x*y

print(ret1)
print(ret2)

  

  偏函数 把函数的参数固定.

posted on 2018-12-26 19:41  哎呀!土豆  阅读(114)  评论(0编辑  收藏  举报

导航