collections deque详解

来源:互联网 发布:隐形眼镜注意事项知乎 编辑:程序博客网 时间:2024/05/17 21:55

deque能把字符串拆开成一个一个字符

from collections import dequed = deque('hello')for elem in d:    print(elem.upper())

结果:

HELLO
d.append('j')  #默认在最后面添加d.appendleft('N')  #在前面添加d
结果:
deque(['N', 'h', 'e', 'l', 'l', 'o', 'j'])

d.pop() #默认去除最后一个d.popleft() #默认去除第一个d
deque(['h', 'e', 'l', 'l', 'o'])

d.extend('abc')  #一次添加多个元素d
deque(['h', 'e', 'l', 'l', 'o', 'a', 'b', 'c'])





原创粉丝点击