[英]How to get pandas series Value counts with a series in original index order after value preference
[英]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.