如何在python中将列表中的最后一项移到最前面? [英] How do I move the last item in a list to the front in python?

查看:67
本文介绍了如何在python中将列表中的最后一项移到最前面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了彻底搜索,但没有找到与此确切内容相关的任何内容.我有一个清单:

I searched thoroughly but can't find anything relating to this exact specific. I have a list:

a = [two, three, one]

我想将 one 移到最前面,所以它变成:

I want to move one to the front, so it becomes:

a = [one, two, three]

问题是,列表中可以是任意数量的数字.假设无法知道是否会有50个项目或3个项目.

The thing is, it could be ANY amount of numbers in the list. Assume there is no way of knowing whether there will be 50 items or 3.

推荐答案

基本上:

a.insert(0, a.pop())

考虑使用 collections.deque 不过,如果您经常这样做.

Consider using collections.deque if you're doing that often, though.

这篇关于如何在python中将列表中的最后一项移到最前面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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