簡體   English   中英

在python中,如何在使用random()函數隨機化后獲取列表或數組或系列的原始順序?

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

使用 random() 函數將它們隨機化后,我無法獲得列表或數組或系列的原始順序? 請建議一些解決方案或不同的算法。

一種解決方案是:

import random

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

您也可以僅使用索引進行隨機化。 當您想避免操作數據時,它很有用。

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