Python标准库(三)

    xiaoxiao2021-12-14  22

    本篇主要介绍双端队列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
    转载请注明原文地址: https://ju.6miu.com/read-971567.html

    最新回复(0)