簡體   English   中英

根據 1 個列表創建 2 個隨機列表?

[英]Create 2 random lists based on 1 list?

我有一個 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 行代碼)。

嘗試使用 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]

怎么樣:

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:]

也許我沒有在隨機庫上進行足夠的搜索

如果你沒有找到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

numpy 有一個 function 洗牌可能會有所幫助:

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)

我會使用隨機庫中的示例方法。 它類似於選擇方法,但不允許重復。 最后,我將所有不在 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

如果您的重新分區是概率(而不是項目的一部分),則需要將其用作閾值以將項目隨機放置在第一個或第二個列表中:

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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM