Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to shuffle pairs within a list?

I have a pairs of names in a list that I want to shuffle for xgboost.

Input: [['A1', 'B1'], ['A2', 'B2'], ['A3', 'B3'], ['A4', 'B4'], ['A5', 'B5'], ['A6', 'B6'], ['A7', 'B7'], ['A8', 'B8']]

Expecting: [['A1', 'B1'], ['B2', 'A2'], ['A3', 'B3'], ['A4', 'B4'], ['B5', 'A5'], ['A6', 'B6'], ['A7', 'B7'], ['B8', 'A8']]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have tried this and it does produce the results I want but I believe there’s an easy way.:

rand_number = random.randint(0, 1000)
tlist_rand = []
for t in tlist:
    if rand_number % 3 == 0:
        tlist_rand.append(random.sample(t, len(t)))
        rand_number = random.randint(0, 1000)
    else:
        tlist_rand.append(t)
        rand_number = random.randint(0, 1000)

Input: [['A1', 'B1'], ['A2', 'B2'], ['A3', 'B3'], ['A4', 'B4'], ['A5', 'B5'], ['A6', 'B6'], ['A7', 'B7'], ['A8', 'B8']]

The rand tuple is: [['A1', 'B1'], ['A2', 'B2'], ['A3', 'B3'], ['B4', 'A4'], ['A5', 'B5'], ['A6', 'B6'], ['A7', 'B7'], ['A8', 'B8']]

>Solution :

You can use random.shuffle, this function can shuffle list inplace.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading