说我有一个清单:
myList = [1,2,3,4,5,6,7,8]
现在,假设我有一个用户输入他们想要关闭列表的索引。如何删除索引大于输入值的列表中的所有元素,或者重新创建具有新边界的列表?
e.g)
x = input('Please enter index of last value in new list')
# say inputted value for x is 3
# I want to end the list at index 3 (i.e the new list will be [1,2,3,4])
获得目标的最快方法是什么?
答案 0 :(得分:-2)
myList = [1,2,3,4,5,6,7,8]
x = input('Please enter index of last value in new list')
myList = myList[0:x+1]
print myList