Python練習題答案: 重排陣【難度:2級】--景越Python編程實例訓練營,1000道上機題等你來挑戰

重排陣【難度:2級】:

答案1:

import numpy as np

def reorder(arr, n):
    a = np.array(arr)
    return np.partition(a, n).tolist()

答案2:

import numpy 
def reorder(arr, n):
    k = numpy.argpartition(arr, n)
    return [ arr[i] for i in k]

答案3:

import numpy as np

def reorder(arr, n):
    
    res = np.argpartition(arr, n)
    
    return [arr[i] for i in res]

答案4:

def reorder(arr, n):
    from numpy import array,partition as pt
    return list(pt(array(arr),n)) # [pt(array(arr),n)]​

答案5:

import numpy as np

reorder = lambda arr, n: list(np.partition(np.array(arr), n))

答案6:

import numpy as np
def reorder(arr, n): return np.partition(arr, n).tolist()

答案7:

import numpy as np


def reorder(arr, n):
    return np.partition(np.array(arr), n).tolist()

答案8:

import numpy as np

def reorder(xs, n):
    return guess_order(xs, n)

def guess_order(xs, n):
    return np.partition(xs, n).tolist()

答案9:

import numpy
reorder = lambda Q,S : list(numpy.partition(Q,S))

答案10:

import numpy as np


def reorder(arr, n):
    return list(np.partition(np.array(arr), n))



Python基礎訓練營景越Python基礎訓練營QQ羣

在這裏插入圖片描述
歡迎各位同學加羣討論,一起學習,共同成長!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章