Better way to shuffle two related lists

后端 未结 7 2216
星月不相逢
星月不相逢 2020-12-13 09:24

Is there better ways to randomly shuffle two related lists without breaking their correspondence in the other list? I\'ve found related questions in numpy.array

7条回答
  •  庸人自扰
    2020-12-13 10:26

    You can do an unzip at the end to limit the awkwardness a bit?

    import numpy as np
    list1 = [1,2,3]
    list2 = [4,5,7]
    list_zipped = list(zip(list1,list2))
    np.random.shuffle(list_zipped)
    list1,list2 = zip(*z) #unzipping
    

提交回复
热议问题