Shuffle two list at once with same order

前端 未结 6 949
感动是毒
感动是毒 2021-01-30 03:38

I\'m using the nltk library\'s movie_reviews corpus which contains a large number of documents. My task is get predictive performance of these reviews

6条回答
  •  抹茶落季
    2021-01-30 04:13

    You can use the second argument of the shuffle function to fix the order of shuffling.

    Specifically, you can pass the second argument of shuffle function a zero argument function which returns a value in [0, 1). The return value of this function fixes the order of shuffling. (By default i.e. if you do not pass any function as the second argument, it uses the function random.random(). You can see it at line 277 here.)

    This example illustrates what I described:

    import random
    
    a = ['a', 'b', 'c', 'd', 'e']
    b = [1, 2, 3, 4, 5]
    
    r = random.random()            # randomly generating a real in [0,1)
    random.shuffle(a, lambda : r)  # lambda : r is an unary function which returns r
    random.shuffle(b, lambda : r)  # using the same function as used in prev line so that shuffling order is same
    
    print a
    print b
    

    Output:

    ['e', 'c', 'd', 'a', 'b']
    [5, 3, 4, 1, 2]
    

提交回复
热议问题