Colors = ["yellow", "red", "green"]
我正在尋找給出串列最新元素的新方法。有沒有不使用索引的方法[-1]?
uj5u.com熱心網友回復:
提供更多背景關系會有所幫助,因為您可以簡單地使用Colors[len(Colors) - 1],但這仍然使用索引。另一種解決方案是Colors.pop(),但請記住,它將洗掉最后一個元素。
uj5u.com熱心網友回復:
您可以next在reversed串列中呼叫:
next(reversed(Colors))
# 'green'
uj5u.com熱心網友回復:
在python中,list類提供了一個函式pop(),你可以使用Colors.pop()
uj5u.com熱心網友回復:
這個怎么樣?
last = None
for x in Colors:
last = x
print last
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/360852.html
