![](/img/trans.png)
[英]How to enumerate over a list in python backwards with index(From the end to the beginning)
[英]Slice list backwards from index 0 in python
假設我有以下列表:
the_list = ['one','two','three','four','five']
如何切片此 python 列表,以便我的輸出如下(打印順序無關緊要):
'one','five','four'
我基本上想從索引 0 開始,然后向后切片
謝謝!
嘗試這個:
the_list[0:1] + the_list[-1:2:-1]
這將添加 2 個列表,其中您的第二個列表從最后一個元素開始,步長為 2,反之亦然。 您不能進行多次切片,但可以選擇要添加或附加的項目。
您可以將第一個元素附加到列表的末尾,然后向后切片,不包括第一個元素:
the_list.append(the_list[0]) [the_list[-i] for i in range(1,len(the_list))]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.