设为首页 加入收藏

TOP

Python记录_day21 模块(一)
2018-11-14 22:08:19 】 浏览:211
Tags:Python 记录 _day21 模块

引入模块的方式:

1. import 模块

2. from xxx import 模块

 

一、collections 模块

1.Counter()

 counter是一个计数器,主要用来计数,计算一个字符串中每个字符出现的次数

1 from collections import Counter
2 s = "我要从南走到北,还要从北走到南"
3 
4 ret  = Counter(s)
5 print("__iter__" in dir(ret))
6 
7 for item in ret:
8     print(item,ret[item])
counter

 

#补充 

栈:先进后出

队列:先进先出

由于python没有给出Stack模块. 我们可以自己动写个粗略版本来模拟栈的工作过程(注意, 此版本有严重的并发问题)

 1 #栈 先进后出
 2 class StackFullErro(Exception):
 3     pass
 4 
 5 class StackEmptyErro(Exception):
 6     pass
 7 
 8 class Stack:
 9     def __init__(self,size):
10         self.size = size
11         self.lst = []
12         self.index = 0
13 
14     def push(self,item):
15         if self.index == self.size:
16             raise StackFullErro("the Stack is full")
17         self.lst.insert(self.index,item)
18         self.index +=1
19 
20     def pop(self):
21         if self.index == 0:
22             raise StackEmptyErro('the stack is empty')
23         self.index -= 1
24         item = self.lst.pop(self.index)
25         return item
26 
27 s = Stack(4)
28 s.push('1')
29 s.push('2')
30 s.push('3')
31 s.push('4')
32 # s.push('5')
33 # s.push('6')
34 print(s.pop())
35 print(s.pop())
36 print(s.pop())
37 print(s.pop())
38 # print(s.pop())
39 
40 结果:
41 4
42 3
43 2
44 1

对于队列,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 # print(q.get())  #拿完了,再拿程序就会阻塞
14 print('拿完了')
15 print(dir(queue))
16 
17 #双向对列
18 q2 = queue.deque()  #创建对象
19 q2.append("")  #在右边添加
20 q2.appendleft("")  #在左边添加
21 
22 # print(q2.pop())  #从右边拿
23 # print(q2.pop())
24 
25 print(q2.popleft())  #从左边拿
26 print(q2.popleft())
queue模块

 

2、deque()

创建双向队列

from collections import deque
q = deque()  #创建双向队列对象

q.append("盖伦")
q.append('皇子')
q.append('赵信')
q.appendleft('德玛西亚之力')
q.appendleft('嘉文')
q.appendleft('德邦总管')
#  德邦 嘉文 德玛 盖伦 皇子 赵信

# print(q.pop())
# print(q.pop())
# print(q.pop())
print(q.popleft())
print(q.popleft())
print(q.popleft())
print(q.popleft())
collections里的deque

 

3、nametuple  

命名元组,就是给元组内的元素进行命名

 1 from collections import namedtuple
 2 
 3 point = namedtuple("",['x','y','z'])  #相当于写了一个类
 4 # print(namedtuple.__doc__)  #Returns a new subclass of tuple with named fields.
 5 p = point(5,2,1)  #相当于创建对象
 6 
 7 print(p.x)  #5
 8 print(p.y)  #2
 9 print(p.z)  #1
10 print(p)  #点(x=5, y=2, z=1)   给元组中每个元素命名了
namedtuple

 

4、OrderedDict  

排序字典,按我们存储的顺序给字典排序

1 dic = {'a':'娃哈哈', 'b':'薯条', 'c':'胡辣汤'}   #无序的
2 print(dic)
3 
4 from collections import OrderedDict
5 od = OrderedDict({'a':'娃哈哈', 'b':'薯条', 'c':'胡辣汤'})  #排序的
6 print(od)
OrderedDict

 

5、defaultdict

默认值字典,查找key时,如果key不存在会返回一个默认值

 1 from collections import defaultdict
 2 
 3 lst = [11,22,33,44,55,66,77,88,99]
 4 
 5 d = defaultdict(list)  #当查找的key不存在是返回一个list(),并将key添    加到d中,所以参数必须是可调用的
 6                        #这相当于给每个key都有一个默认值list()
 7 
 8 for el in lst:
 9     if el <66:
10         d["kry1"].append(el)
11     else:
12         d["key2"].append(el)
13 
14 print(d)
15 
16 def fun():
17     return  "胡辣汤"
18 d2 = defaultdict(fun)   #参数要callable
19 print(d2["key1"])
defaultdict

 

二、time模块

日期格式化的标准:(记到秒就行,其他看看)

%y 两位数的年份表示(00-99)

%Y 四位数的年份表示(000-9999)

%m 月份(01-12)

%d 月内中的一天(0-31)

%H 24

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Python中的not, and, or 下一篇Django 常用字段和参数

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目