2014-12-09 43 views
1

我有兩個列表,我想在其中一個關於另一個屬性的值中進行混洗。例如:python:關於其他屬性的混洗列表

list1 = np.array([1,1,1, 2,2,2, 3,3,3]) # spaces for better understanding 
list2 = np.array([1,2,3, 4,5,6, 7,8,9]) 
result = [4,5,6, 1,2,3, 7,8,9] 

我通過

y = split(list2, len(np.unique(list1))) 
np.random.shuffle(y) 
result = np.array(y).flatten() 

解決了這個問題,我想它也工作的情況下,當在列表1屬性都不在一起。例如:

list1 = np.array([1,2,3,1,2,3,1,2,3]) 
list2 = np.array([1,2,3,4,5,6,7,8,9]) 
result = [2,1,3,5,4,6,8,7,9] 

回答

0

解決它:

uniques = np.unique(list1) 
shuffled = uniques.copy() 
np.random.shuffle(shuffled) 

result = list2.copy() 
for orig, new in zip(uniques, shuffled): 
    result[np.where(list1==orig)] = list2[np.where(list1==new)]