python输出从输入列表中的随机列表

发布于 2025-02-13 20:12:40 字数 875 浏览 1 评论 0原文

我需要从列表1创建3个列表。一个具有70%的值,两个为20%和10%。

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# listOutput70 = select  70% of list1 items(randomly)
# with the remaining create two lists of 20% and 10%

#the output can be something like:

#listOutput70 = [2,7,9,8,4,10,3]
#listOutput20 = [1,5]
#listOutput10 = [6]

我已经有一些代码来生成一个百分比的输出,但仅适用于一个列表。

import random


def selector():

    RandomSelection = []
    mySel = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    PercentEnter= 70
    
    New_sel_nb = len(mySel)*int(PercentEnter)/100
        
    
    while len(RandomSelection) < New_sel_nb:
        randomNumber = random.randrange(0, len(mySel),1)
    
        RandomSelection.append(mySel[randomNumber])
    
        RandomSelection = list(set(RandomSelection))
        
    print(RandomSelection)


selector()
#[2, 3, 6, 7, 8, 9, 10]

I need to create 3 lists from my list1. One with 70% of the values and two with 20% and 10%.

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# listOutput70 = select  70% of list1 items(randomly)
# with the remaining create two lists of 20% and 10%

#the output can be something like:

#listOutput70 = [2,7,9,8,4,10,3]
#listOutput20 = [1,5]
#listOutput10 = [6]

I already have some code to generate a percentage output, but works only for one list.

import random


def selector():

    RandomSelection = []
    mySel = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    PercentEnter= 70
    
    New_sel_nb = len(mySel)*int(PercentEnter)/100
        
    
    while len(RandomSelection) < New_sel_nb:
        randomNumber = random.randrange(0, len(mySel),1)
    
        RandomSelection.append(mySel[randomNumber])
    
        RandomSelection = list(set(RandomSelection))
        
    print(RandomSelection)


selector()
#[2, 3, 6, 7, 8, 9, 10]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方。的韩爷 2025-02-20 20:12:40

random.shuffle()将列表改组。然后使用切片来获取每个百分比。

def selector(percents):
    RandomSelection = []
    mySel = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    random.shuffle(mySel)
    
    start = 0
    for cur in percents:
        end = start + cur * len(mySel) // 100
        RandomSelection.append(mySel[start:end])
        start = end

    return RandomSelection

print(selector([70, 20, 10]))

Shuffle the list with random.shuffle(). Then use slices to get each percentage.

def selector(percents):
    RandomSelection = []
    mySel = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    random.shuffle(mySel)
    
    start = 0
    for cur in percents:
        end = start + cur * len(mySel) // 100
        RandomSelection.append(mySel[start:end])
        start = end

    return RandomSelection

print(selector([70, 20, 10]))
黯然 2025-02-20 20:12:40

使用 numpy.split.split 函数 function usage:

from random import shuffle
from numpy import split as np_split


def selector(og_list, percentages):
    if sum(percentages) != 100:
        raise ValueError("Percentages must sum to 100!")
    percentages.sort()
    splits = [round(len(og_list) * percentages[0] / 100),
              round(len(og_list) * (percentages[0] + percentages[1]) / 100)]
    shuffle(og_list)
    return [list(subset) for subset in np_split(og_list, splits)]

usage:用法:

my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

my_percentages_1 = [10, 20, 70]
my_percentages_2 = [40, 10, 50]
my_percentages_3 = [63, 31, 6]

result_1 = selector(my_list, my_percentages_1)
result_2 = selector(my_list, my_percentages_2)
result_3 = selector(my_list, my_percentages_3)

print(result_1)
print(result_2)
print(result_3)
[[2], [8, 3], [4, 9, 7, 5, 0, 1, 6]]
[[8], [0, 2, 7, 1], [9, 4, 6, 3, 5]]
[[1], [0, 3, 2], [4, 8, 5, 9, 7, 6]]

Using numpy.split function:

from random import shuffle
from numpy import split as np_split


def selector(og_list, percentages):
    if sum(percentages) != 100:
        raise ValueError("Percentages must sum to 100!")
    percentages.sort()
    splits = [round(len(og_list) * percentages[0] / 100),
              round(len(og_list) * (percentages[0] + percentages[1]) / 100)]
    shuffle(og_list)
    return [list(subset) for subset in np_split(og_list, splits)]

Usage:

my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

my_percentages_1 = [10, 20, 70]
my_percentages_2 = [40, 10, 50]
my_percentages_3 = [63, 31, 6]

result_1 = selector(my_list, my_percentages_1)
result_2 = selector(my_list, my_percentages_2)
result_3 = selector(my_list, my_percentages_3)

print(result_1)
print(result_2)
print(result_3)
[[2], [8, 3], [4, 9, 7, 5, 0, 1, 6]]
[[8], [0, 2, 7, 1], [9, 4, 6, 3, 5]]
[[1], [0, 3, 2], [4, 8, 5, 9, 7, 6]]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文