Python标准库(三)
2016-12-03 21:25
190 查看
本篇主要介绍双端队列deque
# encoding=UTF-8 import collections d=collections.deque('abcdefg') print "Deque:",d print "Length",len(d) print "Left end:",d[0] print "Right end",d[-1] d.remove('c') print "remove(c):",d #从右端插入 d1=collections.deque() d1.extend('abcdefg') print d1 d1.extend('h') print d1 #从左端插入 d2=collections.deque() d2.extendleft('abcdefg') print d2 d2.extendleft('h') print d2 d2.appendleft('i') print d2 #从右端依次弹出指定的元素 d=collections.deque('abcdefg') while True: try: print d.pop() except IndexError: break print #从左端依次弹出每一个元素 d=collections.deque('abcdefg') while True: try: print d.popleft() except IndexError: break; print #deque可以按照指定的方向旋转然后绕过一些元素: d=collections.deque(xrange(10)) print d d.rotate(2) #正数表示将最后的几个数放到前面 print d d.rotate(-2) #负数表示将开始几个数放到后面 print d d.rotate(-2) print d
相关文章推荐
- 《python标准库》学习笔记2
- Python标准库urllib2的使用细节
- Python标准库中的Tkinter模块
- Python标准库10 多进程初步 (multiprocessing包)
- Python标准库01 正则表达式 (re包)
- 《Python标准库》 目录
- python标准库
- Python标准库01 正则表达式 (re包)
- Python标准库10 多进程初步 (multiprocessing包)
- python标准库09 当前进程信息 (os包)
- Python标准库(一)
- Python标准库(6)
- python标准库
- Python标准库内置函数complex介绍
- Python标准库12 数学与随机数 (math包,random包)
- Python标准库14 数据库 (sqlite3)
- python标准库
- Python标准库10 多进程初步 (multiprocessing包)
- Python标准库
- Python标准库(2)——数据结构之Counter