本文介绍了将条目添加到列表的开头并删除最后一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的list
大约有40个条目.而且我经常想将一个项目附加到列表的开头(标识为0),并想要删除列表的last
条目(标识为40).
I have a list
of about 40 entries. And I frequently want to append an item to the start of the list (with id 0) and want to delete the last
entry (with id 40) of the list.
我该如何做到最好?
包含5个条目的示例:
[0] = "herp"
[1] = "derp"
[2] = "blah"
[3] = "what"
[4] = "da..."
在添加"wuggah"
并最后删除之后,应该像这样:
after adding "wuggah"
and deleting last it should be like:
[0] = "wuggah"
[1] = "herp"
[2] = "derp"
[3] = "blah"
[4] = "what"
我不想结束手动将所有条目逐个移动到下一个ID的情况.
And I don't want to end up manually moving them one after another all of the entries to the next id.
推荐答案
使用收藏.deque :
>>> import collections
>>> q = collections.deque(["herp", "derp", "blah", "what", "da.."])
>>> q.appendleft('wuggah')
>>> q.pop()
'da..'
>>> q
deque(['wuggah', 'herp', 'derp', 'blah', 'what'])
这篇关于将条目添加到列表的开头并删除最后一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!