如何在列表中从x到y删除?

2024-09-28 13:09:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道如何从索引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 = 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.")

Tags: ortextnoneinputindexifisnot
1条回答
网友
1楼 · 发布于 2024-09-28 13:09:16

how to delete multiple values from index 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]

相关问题 更多 >

    热门问题