码迷,mamicode.com
首页 > 编程语言 > 详细

python 内置模块collections

时间:2019-05-04 09:28:12      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:imp   make   name   www.   .com   模块   website   websites   follow   

1、namedtuple

from collections import namedtuple

websites = [
(‘Sohu‘, ‘http://www.google.com/‘, u‘张朝阳‘),
(‘Sina‘, ‘http://www.sina.com.cn/‘, u‘王志东‘),
(‘163‘, ‘http://www.163.com/‘, u‘丁磊‘)
]

Website = namedtuple(‘Website‘, [‘name‘, ‘url‘, ‘founder‘])
mywebsite = Website(‘ading‘, ‘http://blog.65535.fun‘, u‘林天来‘)
print(mywebsite.name)
print(mywebsite.url)
print(mywebsite.founder)
for website in websites:
website = Website._make(website)
print website

2、deque
In [1]: from collections import deque

In [2]: mydeque = deque(maxlen=10)

In [3]: mydeque.maxlen
Out[3]: 10

In [4]: mydeque.append(1)

In [5]: mydeque.append(10)

In [6]: mydeque
Out[6]: deque([1, 10])

In [7]: mydeque.appendleft(‘a‘)

In [8]: mydeque.appendleft(‘b‘)

In [9]: mydeque
Out[9]: deque([‘b‘, ‘a‘, 1, 10])

In [10]: mydeque.pop()
Out[10]: 10

In [11]: mydeque
Out[11]: deque([‘b‘, ‘a‘, 1])

In [12]: mydeque.popleft()
Out[12]: ‘b‘

In [13]: mydeque
Out[13]: deque([‘a‘, 1])

deque 还有常用的方法 :copy,reverse, insert, clear

python 内置模块collections

标签:imp   make   name   www.   .com   模块   website   websites   follow   

原文地址:https://blog.51cto.com/12080420/2388662

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