简体   繁体   中英

In python, how to get original order of list or array or series after randomizing them using random() function?

I'm unable to get original order of list or array or series after randomizing them using random() function ? Please suggest some solution or different algorithm for the same.

One solution is:

import random

listA = [1,2,3,4]
listB = listA.copy()
random.shuffle(listA)
print(listA, listB)

You can also randomize using index only. It's useful when you want to avoid the manipulation of data.

import random

listA = ['A','B','C','D']

idxs = [i for i in range(len(listA))]
random.shuffle(idxs)

listB = [listA[i] for i in idxs]

print(listA, listB)

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