fruits = ['apple', 'oranges', 'pineapples', 'mangoes', 'avocado', 'berries']
result = [fruits[-2], fruits[-1]] #Last and second last items
result2 = fruits[-2:-1] #All items between second last and the last items
result3 = fruits[-2:] #All items from the second last
Python提供了许多使用自定义索引、条件管理列表的方法
所有这些变量将包含相同的元素。在我看来,你应该使用第三个:
相关问题 更多 >
编程相关推荐