2016-07-14 97 views

回答

2

你可以洗牌之前zip的名單,然後進行解壓縮。這不是特別有效的內存(因爲你在洗牌過程中基本上是複製列表)。

a = [1,2,3] 
b = [4,5,6] 

c = zip(a, b) 
random.shuffle(c) 

a, b = zip(*c) 
2

隨着熊貓,你會置換而不是指數:

df = pd.DataFrame(np.random.choice(list('abcde'), size=(10, 2)), columns = list('AB')) 

df 
Out[39]: 
    A B 
0 c e 
1 b e 
2 c e 
3 a d 
4 e d 
5 d d 
6 b b 
7 a e 
8 e b 
9 a b 

採樣與frac=1爲您提供了洗牌後的數據框:

df.sample(frac=1) 
Out[40]: 
    A B 
6 b b 
5 d d 
1 b e 
2 c e 
9 a b 
8 e b 
4 e d 
7 a e 
3 a d 
0 c e 
+0

@JF我張貼,因爲OP說,他是開放的大熊貓的解決方案。 – ayhan

3

你可以洗牌指數列表:

import random 

def reorderList(l, order): 
    ret = [] 
    for i in order: 
     ret.append(l[i]) 
    return ret 

order = random.shuffle(range(len(a))) 
a = reorderList(a, order) 
b = reorderList(b, order)