Shuffle two list at once with same order

前端 未结 6 704
感动是毒
感动是毒 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:08

    Easy and fast way to do this is to use random.seed() with random.shuffle() . It lets you generate same random order many times you want. It will look like this:

    a = [1, 2, 3, 4, 5]
    b = [6, 7, 8, 9, 10]
    seed = random.random()
    random.seed(seed)
    a.shuffle()
    random.seed(seed)
    b.shuffle()
    print(a)
    print(b)
    
    >>[3, 1, 4, 2, 5]
    >>[8, 6, 9, 7, 10]
    

    This also works when you can't work with both lists at the same time, because of memory problems.

提交回复
热议问题