[英]I have to create a function that returns True or False if a list is sorted
[英]I am writing a function which accepts two lists of same size, if one list is the reverse of the other the function return True else it returns False?
len(lst2) - 1
的值是 lst2 可能的最大索引值,因為索引在給定列表的[0, ..., len(list)]
范圍內。 list[len(list) - 1] == list[-1]
。 因此,如果他將遞增 1 的index
減去 1,則意味着他將從最后一個索引轉到前一個索引,依此類推......
注意:您也可以通過簡單地反轉第二個來比較兩個列表:
def reversed_list(lst1, lst2):
return lst1 == lst2[::-1]
print(reversed_list([1, 2, 3], [3, 2, 1])) # True
print(reversed_list([1, 5, 3], [3, 2, 1])) # False
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.