简体   繁体   中英

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.

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] . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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