簡體   English   中英

反轉python 3中的列表

[英]Reversing a list in python 3

使用python 3,我需要反轉給定編號的列表中的元素(尚待確定)。 例如:

list1 = edud - will become dude
list2 = mobbed - will become bombed
list3 = edud - remains the same

只能使用范圍和附加函數,不得使用反向函數,而需要在python循環中使用。 建議?

到目前為止,我有這個,但這是錯誤的:

def reverse(list, number):
    for i in range (0,length(my_list)-1):
            for num in number:
                if num == number:
                    new_list.append(my_list[i-num])
    return new_list

好吧,你的問題讓我有些困惑。 “反向”是從頭到尾讀取列表。 這就是說,的逆轉版本mobbed應該是debbom 不過,在Python中完成此操作的最簡單方法是使用slices

基礎:

a = 'mother'
a[::-1] = 'rehtom'
a[2:4] = 'th'
a[2:5] = 'the'

您嘗試進行的任何類型的轉換都可以通過切片和重新拼接來完成

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM