我想使用下列代码实现通过随机数使得不同出现概率的文字依随对应概率出现。
def random_pick(list, list_j):
x = random.random()
#print(x)
g = 0.e-15
for item, item_p in zip(list, list_j):
g += item_p
#print(g)
if x < g:
break;
return item
当进行循环使用该函数时,有时会出现UnboundLocalError: local variable 'item' referenced before assignment错误,请问应该如何解决?
item 是啥, 你想返回啥?
是 x<g 时, 就返回 item 吗?