0

给定照片列表:

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

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

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

什么是一种简单的鲁棒算法,可以产生如此美观的随机选择?

4

1 回答 1

4
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
于 2012-09-05T06:44:57.257 回答