设为首页 加入收藏

TOP

第二十一天- 基本模块(一)
2018-11-15 00:08:27 】 浏览:171
Tags:二十一 基本 模块

 

# 模块:模块就是我们把装有特定功能的代码进?归类的结果
# 目前写的所有的py?件都是模块

# 引入方式:
# 1.import 模块
# 2.from xxx import 模块
# 目前都是用的python内置模块 后面高级框架时需要额外安装第三方模块


# collections模块:
# 1.Counter 计数器 计数
 1 from collections import Counter
 2 
 3 s = "狗咬我一口,难道我还要去咬狗?"
 4 # dic = {}
 5 # for el in s:
 6 #     dic[el] = dic.setdefault(el,0) + 1
 7 # print(dic)
 8 
 9 c = Counter(s)  # 获取到的结果可像字典一样使用
10 # print("__iter__"in dir(c))
11 print(c)
12 # for k in c:
13 #     print(k, c[k])
View Code

 

# 2.双向队列
# 数据结构: 栈 先进后出 队列 先进先出
 1 # python中提供了列队 没有栈 自己写一个栈
 2 class StackFullError(Exception):
 3     pass
 4 class StackEmptyError(Exception):
 5     pass
 6 
 7 class Stack:
 8     def __init__(self, size):
 9         self.index = 0 #  栈顶指针
10         self.lst = []
11         self.size = size
12 
13     # 给栈添加元素
14     def push(self, item):
15         if self.index == self.size:
16             # 栈已经满了. 不能再装东西了
17             raise StackFullError('the stack is full')
18         self.lst.insert(self.index, item) # 对于空列表. 需要insert插入内容
19         # self.lst[self.index] = item # 把元素放到栈里
20         self.index += 1     # 栈顶指针向上移动
21 
22     # 从栈中获取数据
23     def pop(self):
24         if self.index == 0:
25             raise StackEmptyError("the stack is empty")
26         self.index -=1 # 指针向下移动
27         item = self.lst.pop(self.index) # 获取元素. 删除.
28         return item
29 
30 # s = Stack(5)
31 # s.push("馒头1号")
32 # s.push("馒头2号")
33 # s.push("馒头3号")
34 # s.push("馒头4号")
35 # s.push("馒头5号")
36 #
37 # print(s.pop())
38 # print(s.pop())
39 # print(s.pop())
40 # print(s.pop())
41 # print(s.pop())
42 #
43 #
44 # lst = []
45 # lst.append("哈哈1")
46 # lst.append("哈哈2")
47 # lst.append("哈哈3")
48 # lst.append("哈哈4")
49 #
50 # print(lst.pop())
51 # print(lst.pop())
52 # print(lst.pop())
53 # print(lst.pop())
View Code
# 队列  先进先出
# python提供了queue模块. 直接用
 1 import queue
 2 
 3 q = queue.Queue()
 4 q.put("林俊杰")
 5 q.put("王力宏")
 6 q.put("周杰伦")
 7 q.put("陈道明")
 8 
 9 # print(q.get())
10 # print(q.get())
11 # print(q.get())
12 # print(q.get())
13 #
14 # # print(q.get()) # 若队伍里没有元素了, 继续获取 会阻塞
15 # input() # 也会阻塞 输入等待 enter就能结束
16 # print("没有了。。。")
View Code
# deque 注意,此列队是collection中的
 1 from collections import deque
 2 
 3 q = deque()
 4 
 5 q.append("刘亦菲")
 6 q.append("江疏影")
 7 q.appendleft("杨幂")
 8 q.appendleft("胡歌")
 9 
10 # print(q.pop())  # 从右边拿
11 # print(q.pop())
12 # print(q.popleft())  # 从左边拿
13 # print(q.popleft())
14 
15 # print(q.pop())  # 若队伍里没有元素了 会报错
View Code

 

# 3.namedtuple  命名元祖
 1 from collections import namedtuple
 2 
 3 point = namedtuple("",["x","y","z"])  # 等同一个只有属性的类
 4 # class point:
 5 #     def __init__(self,x,y,z):
 6 #         self.x = x
 7 #         self.y = y
 8 #         self.z = z
 9 
10 p = point(5,6,7)
11 print(p.x)
12 print(p.y)
13 print(p.z)
14 
15 print(p)
16 p.x = 21  # 报错 不可改 终归是一个元祖
View Code

 

# 4.defaultdict
# 可以给字典设置默认值. 当key不存在时. 直接获取默认值:
 1 from collections import   defaultdict
 2 
 3 lst = [11,22,33,44,55,66]
 4 d = defaultdict(list)
 5 for el in lst:
 6     if el <22:
 7         d['key1'].append(el)  # key1默认是不存在的. 但是可以拿key1. 一个空列表.
 8     else:
 9         d["key2"].append(el)
10 
11 print(d)
View Code

 

# 5.orderdict
# orderdict 字典默认无序 OrderedDict是有序的
1 dic = {'a':'娃哈哈', 'b':'薯条', 'c':'胡辣汤'}
2 print(dic)
3 from collections import OrderedDict
4 od = OrderedDict({'a':'娃哈哈', 'b':'薯条', 'c':'胡辣汤'})
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇python包导入细节 下一篇python学习笔记-字典

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目