从某个元素开始的Python重新排列列表 [英] Python rearrange list starting from a certain element

查看:26
本文介绍了从某个元素开始的Python重新排列列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个python列表和一个项目的索引,我想从索引后的元素开始在列表上开始循环.例如,我有:

I have a python list and an index of an item I want to start a loop on the list starting from the element after my index. For example I have:

original_list = [1,2,3,4,5]
my_index = 2
new_list = [4,5,1,2,3]

我正在尝试获取新列表.

I am trying to achieve the new list.

推荐答案

只需使用列表切片,就像这样

Just use list slicing, like this

>>> original_list, my_index = [1, 2, 3, 4, 5], 2
>>> original_list[my_index + 1:] + original_list[:my_index + 1]
[4, 5, 1, 2, 3]

这篇关于从某个元素开始的Python重新排列列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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