简体   繁体   English

在python中,如何在使用random()函数随机化后获取列表或数组或系列的原始顺序?

[英]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 ?使用 random() 函数将它们随机化后,我无法获得列表或数组或系列的原始顺序? 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)

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

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