2015-01-21 80 views

回答

0

讓我們NumPy的做吧!

import numpy as np 

A = np.random.randint(0, 100, 100) # or np.array(A) to convert from list 
B = np.random.randint(0, 100, 100) 

diff = np.abs(A-B) # your sort order 
sortidx = np.argsort(diff) # indexes which sort the data by diff 

print A[sortidx] # print A sorted in order of abs(A-B) 
print B[sortidx] 

如果你喜歡不NumPy的(見Equivalent of Numpy.argsort() in basic python?):

import operator 

diff = map(operator.sub, A, B) 
sortidx = sorted(range(len(diff)), key=diff.__getitem__) 

print [A[idx] for idx in sortidx] 
print [B[idx] for idx in sortidx] 
+0

先生,你能告訴我什麼是NumPy的?我是python的初學者 – user220789 2015-01-21 06:57:21

+0

請嘗試Google。如果NumPy令人害怕,我也發佈了一個常規的Python解決方案。 – 2015-01-21 07:01:01