我有一個 global_list:[1,7,23,72,7,83,60]
我需要從這個(示例)中創建 2 個隨機串列:
- list_1: [7, 7, 23, 83, 72]
- list_2: [1, 60]
我的實際作業代碼是:
import random
import copy
def get_2_random_list(global_list, repartition):
list_1, list_2 = [], copy.copy(global_list)
list_1_len = round(len(list_2)*repartition)
print("nbr of element in list_1:",list_1_len)
for _ in range(list_1_len):
index = random.randint(1,len(list_2)-1)
list_1.append(list_2[index])
del list_2[index]
return list_1, list_2
global_list = [1,7,23,72,7,83,60]
list_1,list_2 = get_2_random_list(global_list,0.7)
print("list_1:", list_1)
print("list_2:", list_2)
print("global_list:", global_list)
感覺可以優化一下。(也許我在random庫中搜索得不夠多)在效率(我正在處理數百萬個元素)和密度方面(我更愿意為該函式使用 1 或 2 行代碼)。
uj5u.com熱心網友回復:
def get_2_random_list(global_list, repartition):
g_list = list(global_list)
random.shuffle(g_list)
split_point = round(len(g_list)*repartition)
return g_list[:split_point], g_list[split_point:]
uj5u.com熱心網友回復:
嘗試使用 NumPy:
l = [1,7,23,72,7,83,60]
l2 = l.copy()
# randomly select a number based on the len of the list
split_num = np.random.choice(len(l), 1)
# create a new list by using random choice without replacement
l1 = list(np.random.choice(l, split_num, replace=False))
# remove the numbers in l1 from the original list
[l2.remove(x) for x in l1]
# print your two new lists
print(l1)
print(l2)
print(l)
[60, 83, 23, 72]
[1, 7, 7]
[1, 7, 23, 72, 7, 83, 60]
uj5u.com熱心網友回復:
也許我在隨機庫上搜索得不夠
如果你沒有找到,你肯定沒有足夠的搜索 random.sample
def get_2_random_list(global_list, repartition):
list_1_len = int(round(len(global_list)*repartition))
list1 = random.sample(global_list, list_1_len)
set1 = set(list1)
list2 = [element for element in global_list if element not in set1]
return list1, list2
編輯:如果您的串列項不是唯一的,這將是生成 list2 的更好方法:
def get_2_random_list(global_list, repartition):
global_len = len(global_list)
list_1_len = int(round(global_len*repartition))
list1_indices = random.sample(range(global_len), list_1_len)
list1 = [global_list[idx] for idx in list1_indices]
set1 = set(list1_indices)
list2 = [element for idx, element in enumerate(global_list)
if idx not in set1]
return list1, list2
附錄:如果您對 list2 中元素的順序不感興趣,使用 random.shuffle 會更快。
def get_2_random_list(global_list, repartition):
list_1_len = int(round(len(global_list)*repartition))
lstcopy = list(global_list)
random.shuffle(lstcopy)
list1 = lstcopy[:list_1_len]
list2 = lstcopy[list_1_len:]
return list1, list2
uj5u.com熱心網友回復:
我會使用隨機庫中的示例方法。它類似于選擇方法,但不允許重復。最后,我將不在 list_1 中的每個專案添加到 list_2。
def get_2_random_list(global_list, repartition):
list_1_len = round(len(global_list) * repartition)
list_1 = random.sample(global_list, list_1_len)
list_2 = [i for i in global_list if i not in (list_1)]
return list_1, list_2
uj5u.com熱心網友回復:
如果您的重新分配是一個概率(而不是專案的比例),您需要將其用作閾值以將專案隨機放置在第一個或第二個串列中:
import random
L = [1,7,23,72,7,83,60]
repartition = 0.7 # as a probability
L1,L2 = [],[]
for n in L:
(L1,L2)[random.random()>repartition].append(n)
print(L1) # [23, 72, 60]
print(L2) # [1, 7, 7, 83]
這意味著專案有 70% 的機會被放在第一個串列中,30% 的機會進入第二個串列。對于短串列,這通常甚至不會接近 70/30 的專案比例。
如果您的重新磁區以專案數表示比例,則可以使用 random.sample 來避免修改原始串列:
from random import sample
L = [1,7,23,72,7,83,60]
repartition = 0.7 # as a proportion of items
L1,L2 = (lambda R,p:(R[:p],R[p:]))(sample(L,len(L)),round(repartition*len(L)))
print(L1) # [7, 83, 7, 72, 60]
print(L2) # [23, 1]
uj5u.com熱心網友回復:
NumPy 有一個功能 shuffle 可能會有所幫助:
arr = np.array([1,7,23,72,7,83,60])
np.random.shuffle(arr)
p = 5
a1 = arr[:p]
a2 = arr[p:]
print(a1)
print(a2)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/360855.html
