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