【问题标题】:How to slice a list with START being larger than STOP如何对 START 大于 STOP 的列表进行切片
【发布时间】:2015-08-23 03:30:57
【问题描述】:

我想从 START 到 STOP 分割一个列表,但是让迭代继续经过最后一个元素,然后一直循环回到第一个元素。然后继续步进,直到到达 STOP。

我尝试过这样的列表切片:

    list_a = list(range(8))
    reordered_list = list_a[5:2]

但只收到一个空列表:

    []

理想情况下,我希望 list_a 打印出来:

    [5, 6, 7, 0, 1]

基本上,我希望遍历“整圈”并在索引 0 处继续迭代。

【问题讨论】:

    标签: python


    【解决方案1】:
    def parse(input, start, stop):
        partial_list = input[:stop]
        return partial_list[start:] + partial_list[:start]
    
    full_list = range(8)
    parse(full_list, 2, 5)  # Outputs [2, 3, 4, 0, 1]
    

    编辑:您的问题有些不一致,如果可能,请改写。

    【讨论】:

      【解决方案2】:

      当你给出一个切片索引时 -

      list_a[5:2]
      

      步长为 1,并且由于开始在停止之后,因此这些索引之间没有元素(切片不会自行循环)。

      您应该使用modulus(余数)运算符和for 循环或列表推导来获得您想要的列表。示例 -

      def foo(list_a, start, stop):
          if start >= len(list_a):
              start = len(list_a) - 1
          if stop >= len(list_a):
              stop = len(list_a) - 1
          i = start
          ret = []
          while i != stop:
              ret.append(list_a[i])
              i = (i+1)%len(list_a)
          return ret
      

      示例/演示 -

      >>> def foo(list_a, start, stop):
      ...     if start >= len(list_a):
      ...         start = len(list_a) - 1
      ...     if stop >= len(list_a):
      ...         stop = len(list_a) - 1
      ...     i = start
      ...     ret = []
      ...     while i != stop:
      ...         ret.append(list_a[i])
      ...         i = (i+1)%len(list_a)
      ...     return ret
      ...
      >>> foo(list(range(8)),5,2)
      [5, 6, 7, 0, 1]
      

      【讨论】:

        【解决方案3】:

        这样做:

        def foo(list_a, start, stop):
            if (start <= stop):
                return list_a[start:stop]
            else:
                return list_a[start:] + list_a[:stop]
        

        输出:

        >>> x = list(range(8))
        >>> foo(x, 5, 2)
        [5, 6, 7, 0, 1]
        

        【讨论】:

        • 感谢 DrNightmare!我喜欢你简洁的回答:)
        【解决方案4】:

        使用取模运算符,%:

        >>> l = list(range(8))
        >>> start = 5
        >>> while start%len(l) != 2:
        ...     print(l[start%len(l)])
        ...     start += 1
        ...
        5
        6
        7
        0
        1
        

        作为一个函数:

        >>> def foo(l, start, end):
        ...     result = []
        ...     while start%len(l) != end:
        ...         result.append(l[start%len(l)])
        ...         start += 1
        ...     return result
        ...
        >>> foo(l, 5, 2)
        [5, 6, 7, 0, 1]
        

        【讨论】:

          【解决方案5】:

          模运算符在这里是你的朋友。每当您请求列表的索引时,请请求 [index] mod [size of list]。

          MyList[i%len(Mylist)]
          

          在您的情况下,如果结束索引小于开始索引,请编写一个检查,将列表的大小添加到结束索引,然后使用我建议的代码。

          【讨论】:

            【解决方案6】:

            这对我有用:

            >>> reordered = (list_a[5:]+list_a[:2])
            >>> print (reordered)
            >>> [5, 6, 7, 0, 1]
            

            基本上是切片然后将它们相加。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2022-01-17
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-10-20
              • 2014-07-08
              • 2020-08-27
              相关资源
              最近更新 更多