Python 如何在列表中从x删除到y?
我想知道如何从索引x到y删除多个值, 这就是我目前正在尝试的:Python 如何在列表中从x删除到y?,python,list,Python,List,我想知道如何从索引x到y删除多个值, 这就是我目前正在尝试的: first_num = None second_num = None while True: first_or_last = "first" if first_num is None else "last" text = str("Enter the {} index number to start deleting: ").format(first_or_last) remove_ftl_input = inp
first_num = None
second_num = None
while True:
first_or_last = "first" if first_num is None else "last"
text = str("Enter the {} index number to start deleting: ").format(first_or_last)
remove_ftl_input = input(text)
if first_num is None or second_num is None:
if remove_ftl_input.isdigit():
if first_num is None:
first_num = int(remove_ftl_input)
elif first_num is not None and second_num is None:
second_num = int(remove_ftl_input)
if first_num is not None and second_num is not None:
for x in range(0, first_num-second_num):
try:
# note: every loop index shifts by -1 thats why first-num i assume?
found_items_list.pop(first_num)
except IndexError:
print(str(x) + " was out of reach.")
如何从索引x中删除多个值
为什么不加入你想要保留的范围呢
>>> list = [0, 1, 2, 3, 4, 5, 6, 7]
>>> first = 3
>>> last = 4
>>> list = list[:first] + list[last+1:]
>>> list
[0, 1, 2, 5, 6, 7]
您想删除索引
x
中的一个数字并将其放入索引y
?您可以在迭代列表时从列表中删除元素,只要从右向左操作即可。删除最右边的项不会更改其余项的索引<代码>用于反转的x(范围(0,第一个数值-第二个数值))应该工作: