简体   繁体   English

随机匹配 Python 中的项目

[英]Randomly match items in Python

I was wondering if there was an algorithm I could find or a data structure that I could use that would enable me to efficiently randomly pair two sets of data with each other.我想知道是否有我能找到的算法或我可以使用的数据结构,使我能够有效地随机配对两组数据。

Initially, I searched for an algorithm/data structure that would do this for me, but it was to no avail, most likely because I do not know the exact terms to search for.最初,我搜索了可以为我执行此操作的算法/数据结构,但无济于事,很可能是因为我不知道要搜索的确切术语。 Currently, I'm using the randint() function from the random library in python to implement my own type of algorithm, however, I'm worried if there may be issues with it not being random enough.目前,我正在使用 python 中random库中的randint() function 来实现我自己的算法类型,但是,我担心它是否可能存在不够随机的问题。

With the information you have provided in the question, my approaches would be as follows:根据您在问题中提供的信息,我的方法如下:

Approach 1:方法一:

Lets say you have 2 datasets A and B and an indexing operator [] , you could use randint to generate 2 random numbers a and b and pair A[a] with B[b] .假设您有 2 个数据集AB以及一个索引运算符[] ,您可以使用randint生成 2 个随机数ab并将A[a]B[b]配对。 Here the advantage is you can bootstrap as many times you want to generate a dataset as big as you want.这里的优点是您可以引导任意多次,以生成任意大小的数据集。

Approach 2:方法二:

As @Mark Meyer suggested in the comments, shuffle the 2 datasets separately, and then pair each data point.正如@Mark Meyer 在评论中建议的那样,分别打乱两个数据集,然后对每个数据点进行配对。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM