随机生成前N个选择

时间:2012-09-05 06:37:08

标签: python random

给出一系列照片:

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

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

我想向用户显示10张图片。我想要它,以便第1组最有可能在列表中占主导地位(比如说,10个图像中的6个),而其他组则因为数量较少而不是受到不公平惩罚

什么是简单的鲁棒算法,可以产生美学上令人愉悦的随机选择?

1 个答案:

答案 0 :(得分: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