中文字幕一区二区人妻电影,亚洲av无码一区二区乱子伦as ,亚洲精品无码永久在线观看,亚洲成aⅴ人片久青草影院按摩,亚洲黑人巨大videos

Python 將列表中的頭尾兩個元素對調

Document 對象參考手冊 Python3 實例

定義一個列表,并將列表中的頭尾兩個元素對調。

例如:

對調前 : [1, 2, 3]
對調后 : [3, 2, 1]

實例 1

def swapList(newList):
? ? size = len(newList)
? ? ?
? ? temp = newList[0]
? ? newList[0] = newList[size - 1]
? ? newList[size - 1] = temp
? ? ?
? ? return newList

newList = [1, 2, 3]
?
print(swapList(newList))

以上實例輸出結果為:

[3, 2, 1]

實例 2

def swapList(newList):
? ? ?
? ? newList[0], newList[-1] = newList[-1], newList[0]
?
? ? return newList
? ? ?
newList = [1, 2, 3]
print(swapList(newList))

以上實例輸出結果為:

[3, 2, 1]

實例 3

def swapList(list):
? ? ?
? ? get = list[-1], list[0]
? ? ?
? ? list[0], list[-1] = get
? ? ?
? ? return list
? ? ?
newList = [1, 2, 3]
print(swapList(newList))

以上實例輸出結果為:

[3, 2, 1]

Document 對象參考手冊 Python3 實例

其他擴展