【发布时间】:2017-02-10 18:08:09
【问题描述】:
假设我有一个清单:
myList = [1,2,3,4,5,6,7,8]
现在,假设我有一个用户输入了他们想要删除列表的索引。如何删除列表中索引大于输入值的所有元素,或重新创建具有新边界的列表?
例如)
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])
实现我的目标最快的方法是什么?
【问题讨论】:
-
您尝试过什么吗?...请分享您的尝试
-
myList[:] = myList[:int(x)]
-
“我将如何使用与 list.remove() python 函数配对的列表理解” - 将这些东西配对本质上总是一个糟糕的主意,而且这些工具都对您的任务没有用处。跨度>