pizzas = ["hawai","salame","vegetable","capriciosa","new york"]
for pizza in pizzas:
print("I like " + pizza.title() + " pizza!")
print("\n" + "The first three pizzas in the list are: " + str(pizzas[0:3]))
print("\n" + "The last three pizzas in the list are: " + str(pizzas[-1:-3]))
最後三個項目的列表中,我得到:打印在Python
I like Hawai pizza!
I like Salame pizza!
I like Vegetable pizza!
I like Capriciosa pizza!
I like New York pizza!
The first three pizzas in the list are: ['hawai', 'salame', 'vegetable']
The last three pizzas in the list are: []
,我很困惑。是不是-1
表示列表中的最後一個元素?我索引[start:stop]
所以不應該打印我的最後3個項目?我究竟做錯了什麼 ?
你需要的是'比薩餅[ -3:]' – JBernardo