随机生成前N个选择

2024-09-30 22:20:34 发布

您现在位置:Python中文网/ 问答频道 /正文

给出一张照片列表:

set 1 : 14 photos
set 2 : 4 photos
set 3 : 2 photos

[{'set' : 1,'photos' : ['photo1','photo2'....'photo14']}, {'set' : 2,.....]

我想向用户显示10个图像。我希望第1组最有可能在列表中占据主导地位(比如说,10张图片中有6张),而其他组不会因为数量少而受到不公平的惩罚。你知道吗

什么是一个简单的鲁棒算法,结果在这样的审美悦目的随机选择?你知道吗


Tags: 用户图像算法列表数量公平图片照片
1条回答
网友
1楼 · 发布于 2024-09-30 22:20:34
import random
photos = [{'set' : 1,'photos' : ['photo1','photo2'....'photo14']}, {'set' : 2,.....]
weight = {1: 6, 2: 3, 3: 1} # set : number of shown photos
show = []
for d in photos:
    show.extend(random.sample(d['photos'], weight[d['set']]))
random.shuffle(show)
# show now contains a shuffled list of 6 photos from set 1, 3 from set 2 and 1 from set 3

相关问题 更多 >