我有list大约40个条目。而且我经常想将一个项目附加到列表的开头(ID为0),并想要删除列表的last条目(ID为40)。
list
last
我如何做到最好?
5个条目的示例:
[0] = "herp" [1] = "derp" [2] = "blah" [3] = "what" [4] = "da..."
在添加"wuggah"和删除最后一个之后,它应该像:
"wuggah"
[0] = "wuggah" [1] = "herp" [2] = "derp" [3] = "blah" [4] = "what"
而且我不想结束将所有条目逐个手动移动到下一个ID的操作。
使用collections.deque:
>>> import collections >>> q = collections.deque(["herp", "derp", "blah", "what", "da.."]) >>> q.appendleft('wuggah') >>> q.pop() 'da..' >>> q deque(['wuggah', 'herp', 'derp', 'blah', 'what'])