如何在整数列表中对奇数进行排序,但将偶数保留在原始位置?
例:
sortArray([5, 3, 2, 8, 1, 4]) == [1, 3, 2, 8, 5, 4]
Run Code Online (Sandbox Code Playgroud)
我的代码:
def sort_array(source_array):
odd_numbers = [n for n in source_array if n%2!=0]
odd_numbers = sorted(odd_numbers)
Run Code Online (Sandbox Code Playgroud)
如何切换奇数的索引source_array与odd_numbers?
python ×1