python中的pop()函数和popleft()函数
首先对于pop而言,它是用于stack中的:
stack = [1, 2, 3, 4]
print(stack)
stack.append(6)
print(stack)
print(stack.pop(1)) # 可以带参数
print(stack)
运行结果:
[1, 2, 3, 4]
[1, 2, 3, 4, 6]
[1, 3, 4, 6]
而对于pooleft是用于collections中,
from collections import deque
name = deque(["aaa", "bbb", "ccc", "ddd"])
print(name)
name.append("xxx")
print(name)
name.append("yyy")
print(name)
print(name.popleft()) # 不可以带参数
print(name)
运行结果:
deque(['aaa', 'bbb', 'ccc', 'ddd'])
deque(['aaa', 'bbb', 'ccc', 'ddd', 'xxx'])
deque(['aaa', 'bbb', 'ccc', 'ddd', 'xxx', 'yyy'])
deque(['bbb', 'ccc', 'ddd', 'xxx', 'yyy'])