📌  相关文章
📜  Python列表理解 |对偶数元素进行升序排序,奇数元素降序排序

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

Python列表理解 |对偶数元素进行升序排序,奇数元素降序排序

给定一个由 n 个不同数字组成的数组,任务是将所有偶数按递增顺序排序,奇数按递减顺序排序。修改后的数组应包含所有排序的偶数,后跟反向排序的奇数。

请注意,第一个元素被认为是偶数,因为它的索引为 0。

例子:

Input:  arr[] = {0, 1, 2, 3, 4, 5, 6, 7}
Output: arr[] = {0, 2, 4, 6, 7, 5, 3, 1}
Even-place elements : 0, 2, 4, 6
Odd-place elements : 1, 3, 5, 7
Even-place elements in increasing order : 
0, 2, 4, 6
Odd-Place elements in decreasing order : 
7, 5, 3, 1

Input: arr[] = {3, 1, 2, 4, 5, 9, 13, 14, 12}
Output: {2, 3, 5, 12, 13, 14, 9, 4, 1}
Even-place elements : 3, 2, 5, 13, 12
Odd-place elements : 1, 4, 9, 14
Even-place elements in increasing order : 
2, 3, 5, 12, 13
Odd-Place elements in decreasing order : 
14, 9, 4, 1

我们有解决此问题的现有解决方案,请参阅以递增顺序排列偶数元素和以递减顺序排列奇数排列的元素。我们可以使用 List Comprehension 在Python中快速解决这个问题。方法很简单,

  1. 将原始列表分成两部分,一部分包含所有偶数索引元素,另一部分包含所有奇数索引元素。
  2. 现在对包含所有偶数索引元素的列表按升序排序,对包含所有奇数索引元素的列表按降序排序。现在将它们连接起来。
# Function to Sort even-placed elements 
# in increasing and odd-placed in decreasing
# order
  
def evenOddSort(input):
  
     # separate even odd indexed elements list
     evens = [ input[i] for i in range(0,len(input)) if i%2==0 ] 
     odds = [ input[i] for i in range(0,len(input)) if i%2!=0 ] 
  
     # sort evens in ascending and odds in 
     # descending using sorted() method
     print (sorted(evens) + sorted(odds,reverse=True))
  
# Driver program
if __name__ == "__main__":
    input = [0, 1, 2, 3, 4, 5, 6, 7]
    evenOddSort(input)

输出:

[0, 2, 4, 6, 7, 5, 3, 1]