📜  Python|列表中严格递增整数的反向序列

📅  最后修改于: 2022-05-13 01:55:43.252000             🧑  作者: Mango

Python|列表中严格递增整数的反向序列

给定一个整数列表,编写一个Python程序来反转给定列表中连续递增的块的顺序。

例子:

Input : [0, 1, 9, 8, 7, 5, 3, 14]
Output : [9, 1, 0, 8, 7, 5, 14, 3]
Explanation: There are two chunks of strictly
             increasing elements (0, 1, 9) and (3, 14). 

Input : [-5, -3, 0, 1, 3, 5, -2, -12]
Output : [5, 3, 1, 0, -3, -5, -2, -12]
Explanation: Only one chunk of strictly increasing 
             elements exists i.e. (-5, -3, 0, 1, 3, 5).


方法#1:朴素(使用扩展切片)

解决给定问题的一种简单方法是使用扩展切片。我们用空列表初始化两个变量“res”(用于存储最终输出)和块(用于存储递增整数块)。现在使用 for 循环,每次我们检查当前元素是否小于块的最后一个元素。如果是,则使用扩展切片 (block[::-1]) 和清理块 (block[:] = [i]) 将反向块添加到“res”。否则,只需将元素附加到“块”。最后,通过反转块扩展'res'并输出它。

# Python3 program to Reverse order 
# of incrementing integers in list
  
def reverseOrder(lst):
    res = [] 
    block = []
    for i in lst:
  
        # check if the current element is less 
        # than the last element of block 
        if block and i < block[-1]:
  
            # add reversed chunk to 'res'
            res.extend(block[::-1]) 
            block[:] = [i]      
        else:
  
            # append the element to 'block'
            block.append(i)
      
    # extend 'res' by reversing block    
    res.extend(block[::-1])
    return(res)
              
# Driver code
lst = [0, 1, 9, 8, 7, 5, 3, 14]
print(reverseOrder(lst))
输出:
[9, 1, 0, 8, 7, 5, 14, 3]


方法#2:使用列表推导

解决上述问题的一种有效方法是使用列表推导。它首先找出递增整数开始的所有位置,并将它们存储在变量“break_”中。在此之后,所有的块被反转并以子列表的形式进行管理,这些子列表又存储在“块”中。最后,'block' 被解压并返回。

# Python3 program to Reverse order 
# of incrementing integers in list
  
def reverseOrder(lst):
    break_ = [0] + [i for i in range(1, len(lst)) 
    if lst[i-1] > lst[i]] + [len(lst)]
      
    block =[list(reversed(lst[i:j])) 
    for i, j in zip(break_[:-1], break_[1:])]
      
    return(sum(block, [])) 
              
# Driver code
lst = [0, 1, 9, 8, 7, 5, 3, 14]
print(reverseOrder(lst))
输出:
[9, 1, 0, 8, 7, 5, 14, 3]