问题描述
我需要实现随机选择。算法采用[(obj,prob),...]列表作为输入的
。每个(obj,prob)代表
可能是一个对象,obj,应根据其概率选择
概率。为了简化问题,假设概率为概率。是整数,并且所有概率的总和是
等于100.例如,
items = [('''Mary'',30),(''John'',10),('''Tom'',45) ,(''简'',15)]
该算法将采用数字N,并将[(obj,prob),...]列表作为
输入,并随机选择N。基于
概率的对象列表中的
对象。
对于N = 1,这很简单;以下代码足以完成工作。
def foo(项目):
index = random.randint( 0,99)
currentP = 0
for(obj,p)in items:
currentP + = w
if currentP index:
return obj
但是一般情况如何,对于N 1和N< LEN(项目)?是否有一些使用Python标准随机的聪明算法?
打包怎么办?
招数?
谢谢,
Ben
您如何看待这个?
随机导入
N = 100
items = [ (''Mary'',30),(''John'',10),(''Tom'',45),(''Jane'',15)]
the_items = []
for i,j in items:
the_items.extend([i] * j)
histogram = {}
for x in xrange(N):
selected = random.choice(the_items)
打印选择,
如果选择在直方图中:
直方图[选择] + = 1
否则:
直方图[已选择] = 1
打印
打印
for i in histogram:
print''%4s:%d''%(i,histogram [i])
## John Mary Jane汤姆玛丽玛丽汤姆约翰汤姆约翰汤姆
##约翰玛丽玛丽玛丽约翰汤姆汤姆约翰玛丽玛丽汤姆玛丽
## Mary John Tom Jane Jane Jane John Tom Tom汤姆John Tom
## Tom Mary汤姆玛丽汤姆玛丽汤姆汤姆汤姆玛丽玛丽汤姆
##玛丽汤姆玛丽汤姆简汤姆玛丽汤姆简汤姆汤姆汤姆汤姆
##汤姆玛丽汤姆简汤姆玛丽玛丽珍玛丽约翰玛丽玛丽汤姆
##玛丽玛丽汤姆玛丽约翰汤姆汤姆汤姆玛丽珍玛丽汤姆
##玛丽汤姆汤姆简Tom Mary Mary Tom
##
## Jane:11
## John:12
## Mary:32
## Tom:45
否。这不能解决问题。我想要的是一个函数
def randomPick(n,the_items):
将返回来自the_items的n个DISTINCT项目这样返回的n个项目根据他们在the_items中的(item,pro)元素中指定的概率
。
如果我理解正确,以前的两个回复都会一次一个地输出一个项目,而不是一次返回n个DISTINCT
项目。 br />
I need to implement a "random selection" algorithm which takes a list
of [(obj, prob),...] as input. Each of the (obj, prob) represents how
likely an object, "obj", should be selected based on its probability
of
"prob".To simplify the problem, assuming "prob" are integers, and the
sum of all "prob" equals 100. For example,
items = [(''Mary'',30), (''John'', 10), (''Tom'', 45), (''Jane'', 15)]
The algorithm will take a number "N", and a [(obj, prob),...] list as
inputs, and randomly pick "N" objects based on the probabilities of
the
objects in the list.
For N=1 this is pretty simply; the following code is sufficient to do
the job.
def foo(items):
index = random.randint(0, 99)
currentP = 0
for (obj, p) in items:
currentP += w
if currentP index:
return obj
But how about the general case, for N 1 and N < len(items)? Is there
some clever algorithm using Python standard "random" package to do
the trick?
Thanks,
Ben
What do you think of this?
import random
N = 100
items = [(''Mary'',30), (''John'', 10), (''Tom'', 45), (''Jane'', 15)]
the_items = []
for i,j in items:
the_items.extend([i]*j)
histogram = {}
for i in xrange(N):
chosen = random.choice(the_items)
print chosen,
if chosen in histogram:
histogram[chosen] += 1
else:
histogram[chosen] = 1
print
print
for i in histogram:
print ''%4s: %d'' % (i,histogram[i])
## John Mary Jane Tom Tom Mary Mary Tom John John Tom John Tom
## John Mary Mary Mary John Tom Tom John Mary Mary Tom Mary
## Mary John Tom Jane Jane Jane John Tom Jane Tom Tom John Tom
## Tom Mary Tom Tom Mary Tom Mary Tom Tom Tom Tom Mary Mary Tom
## Mary Tom Mary Tom Tom Jane Tom Mary Tom Jane Tom Tom Tom Tom
## Tom Mary Tom Jane Tom Mary Mary Jane Mary John Mary Mary Tom
## Mary Mary Tom Mary John Tom Tom Tom Tom Mary Jane Mary Tom
## Mary Tom Tom Jane Tom Mary Mary Tom
##
## Jane: 11
## John: 12
## Mary: 32
## Tom: 45
What do you think of this?
import random
N = 100
items = [(''Mary'',30), (''John'', 10), (''Tom'', 45), (''Jane'', 15)]
the_items = []
for i,j in items:
the_items.extend([i]*j)
histogram = {}
for i in xrange(N):
chosen = random.choice(the_items)
print chosen,
if chosen in histogram:
histogram[chosen] += 1
else:
histogram[chosen] = 1
print
print
for i in histogram:
print ''%4s: %d'' % (i,histogram[i])
## John Mary Jane Tom Tom Mary Mary Tom John John Tom John Tom
## John Mary Mary Mary John Tom Tom John Mary Mary Tom Mary
## Mary John Tom Jane Jane Jane John Tom Jane Tom Tom John Tom
## Tom Mary Tom Tom Mary Tom Mary Tom Tom Tom Tom Mary Mary Tom
## Mary Tom Mary Tom Tom Jane Tom Mary Tom Jane Tom Tom Tom Tom
## Tom Mary Tom Jane Tom Mary Mary Jane Mary John Mary Mary Tom
## Mary Mary Tom Mary John Tom Tom Tom Tom Mary Jane Mary Tom
## Mary Tom Tom Jane Tom Mary Mary Tom
##
## Jane: 11
## John: 12
## Mary: 32
## Tom: 45
No. That does not solve the problem. What I want is a function
def randomPick(n, the_items):
which will return n DISTINCT items from "the_items" such that
the n items returned are according to their probabilities specified
in the (item, pro) elements inside "the_items".
If I understand correctly, both of the previous replies will output
one item at a time independently, instead of returning n DISTINCT
items at a time.
from random import sample
randomPick = lambda n,its : sample(eval(''+''.join(''[%r]*%r''%p for p in its)),n)
hth :)
这篇关于在Python中实现随机选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!