添加条目列表,并删除在Python中第一个 [英] Add entry to list and remove first one in Python

查看:233
本文介绍了添加条目列表,并删除在Python中第一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大约40项的列表。而我经常要添加到列表中(ID为0)的启动项目,并希望删除末页项(id为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.

我如何做到这一点的最好?

how do i do this the best?

这样的:(例如具有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"

或追加之一,删除第一。

or appending one and deleting first.

和我不想要结束手动移动它们此起彼伏的所有条目下一个ID。

And I don't want to end up manually moving them one after another all of the entries to the next 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'])

这篇关于添加条目列表,并删除在Python中第一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆