用相同的顺序一次洗牌两个清单

Jar*_*čík 71 python sorting shuffle list

我正在使用nltk语料库movie_reviews,其中有很多文档.我的任务是通过预处理数据获得这些评论的预测性能,而无需预处理.但是有问题,在列表documentsdocuments2我有同样的文件,我需要他们打乱,为了保持相同的顺序在这两个列表.我不能单独洗牌,因为每次我洗牌都会得到其他结果.这就是为什么我需要以相同的顺序对其进行一次洗牌,因为我需要在最后比较它们(这取决于顺序).我正在使用python 2.7

示例(实际上是字符串标记化,但它不是相对的):

documents = [(['plot : two teen couples go to a church party , '], 'neg'),
             (['drink and then drive . '], 'pos'),
             (['they get into an accident . '], 'neg'),
             (['one of the guys dies'], 'neg')]

documents2 = [(['plot two teen couples church party'], 'neg'),
              (['drink then drive . '], 'pos'),
              (['they get accident . '], 'neg'),
              (['one guys dies'], 'neg')]
Run Code Online (Sandbox Code Playgroud)

我需要在洗牌后得到这个结果:

documents = [(['one of the guys dies'], 'neg'),
             (['they get into an accident . '], 'neg'),
             (['drink and then drive . '], 'pos'),
             (['plot : two teen couples go to a church party , '], 'neg')]

documents2 = [(['one guys dies'], 'neg'),
              (['they get accident . '], 'neg'),
              (['drink then drive . '], 'pos'),
              (['plot two teen couples church party'], 'neg')]
Run Code Online (Sandbox Code Playgroud)

我有这个代码:

def cleanDoc(doc):
    stopset = set(stopwords.words('english'))
    stemmer = nltk.PorterStemmer()
    clean = [token.lower() for token in doc if token.lower() not in stopset and len(token) > 2]
    final = [stemmer.stem(word) for word in clean]
    return final

documents = [(list(movie_reviews.words(fileid)), category)
             for category in movie_reviews.categories()
             for fileid in movie_reviews.fileids(category)]

documents2 = [(list(cleanDoc(movie_reviews.words(fileid))), category)
             for category in movie_reviews.categories()
             for fileid in movie_reviews.fileids(category)]

random.shuffle( and here shuffle documents and documents2 with same order) # or somehow
Run Code Online (Sandbox Code Playgroud)

ssh*_*124 156

你可以这样做:

import random

a = ['a', 'b', 'c']
b = [1, 2, 3]

c = list(zip(a, b))

random.shuffle(c)

a, b = zip(*c)

print a
print b

[OUTPUT]
['a', 'c', 'b']
[1, 3, 2]
Run Code Online (Sandbox Code Playgroud)

当然,这是一个简单列表的例子,但适应性将与您的情况相同.

希望能帮助到你.祝好运.

  • ...元组...所以只需 a=list(a) 和 b=list(b) (3认同)
  • (菜鸟问题)-*是什么意思? (2认同)
  • @ᔕᖺᘎᕊ,这意味着解压缩c的值,所以它被称为`zip(1,2,3)`而不是`zip([1,2,3])` (2认同)
  • 我之前使用过这个解决方案,最后列出了 `a` 和 `b`。在 Python 3.6.8 中,在同一个示例的末尾,我将 `a` 和 `b` 作为元组。 (2认同)

小智 18

我有一个简单的方法来做到这一点

import numpy as np
a = np.array([0,1,2,3,4])
b = np.array([5,6,7,8,9])

indices = np.arange(a.shape[0])
np.random.shuffle(indices)

a = a[indices]
b = b[indices]
# a, array([3, 4, 1, 2, 0])
# b, array([8, 9, 6, 7, 5])
Run Code Online (Sandbox Code Playgroud)

  • 原始帖子是关于 python 中的普通列表,但我需要一个 numpy 数组的解决方案。你刚刚救了我的一天! (4认同)
  • 看起来 `np.random.permutation(a.shape[0])` 会更简单 (2认同)

YSc*_*arf 14

from sklearn.utils import shuffle

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

a_shuffled, b_shuffled = shuffle(np.array(a), np.array(b))
print(a_shuffled, b_shuffled)

#random output
#['e' 'c' 'b' 'd' 'a'] [5 3 2 4 1]
Run Code Online (Sandbox Code Playgroud)


Lio*_*Lai 7

同时打乱任意数量的列表。

from random import shuffle

def shuffle_list(*ls):
  l =list(zip(*ls))

  shuffle(l)
  return zip(*l)

a = [0,1,2,3,4]
b = [5,6,7,8,9]

a1,b1 = shuffle_list(a,b)
print(a1,b1)

a = [0,1,2,3,4]
b = [5,6,7,8,9]
c = [10,11,12,13,14]
a1,b1,c1 = shuffle_list(a,b,c)
print(a1,b1,c1)
Run Code Online (Sandbox Code Playgroud)

输出:

$ (0, 2, 4, 3, 1) (5, 7, 9, 8, 6)
$ (4, 3, 0, 2, 1) (9, 8, 5, 7, 6) (14, 13, 10, 12, 11)
Run Code Online (Sandbox Code Playgroud)

注意:
shuffle_list()are返回的对象tuples

PS shuffle_list()也可以应用于numpy.array()

a = np.array([1,2,3])
b = np.array([4,5,6])

a1,b1 = shuffle_list(a,b)
print(a1,b1)
Run Code Online (Sandbox Code Playgroud)

输出:

$ (3, 1, 2) (6, 4, 5)
Run Code Online (Sandbox Code Playgroud)


小智 5

简单快速的方法是将 random.seed() 与 random.shuffle() 结合使用。它可以让您多次生成相同的随机顺序。它看起来像这样:

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]
Run Code Online (Sandbox Code Playgroud)

当由于内存问题而无法同时使用两个列表时,这也适用。

  • 不应该是 random.shuffle(a) 吗? (4认同)