Python:Range()作为字典值

Python:Range()作为字典值,python,dictionary,range,Python,Dictionary,Range,假设我有一个如下所示的字典,其中的值是每个键在文本中出现的概率 dict = {'a':0.66,'b':0.07,'c':0.04 and so on so the values of the dict sum up to one} 假设我想构建另一个字典,它具有这些值的范围hasvalue。 因为我们不能将range()与float一起使用,所以我尝试先将所有值乘以100,这样它们就会变成int。 假设我们想用这些值的范围替换它们。例如,“a”将得到一个范围(0,66),“b”范围(

假设我有一个如下所示的字典,其中的值是每个键在文本中出现的概率

   dict = {'a':0.66,'b':0.07,'c':0.04 and so on so the values of the dict sum up to one}
假设我想构建另一个字典,它具有这些值的范围hasvalue。 因为我们不能将range()与float一起使用,所以我尝试先将所有值乘以100,这样它们就会变成int。 假设我们想用这些值的范围替换它们。例如,“a”将得到一个范围(0,66),“b”范围(66,73),“c”(73,77)等等。 我曾尝试通过以下循环来实现这一点,但它不起作用:

start = 0
end = 0
for k,v in dict.items():
   end+=int(v*100)
   range_dict[k]=range(start,end)
   start+=end
谁能帮帮我吗?我真想知道该怎么办才好

如果你改变

start += end

它应该可以工作(在此处使用
xrange
使其更可见):

但是如果as@Satoru.Logic猜测你想要一个加权随机数,有更好的方法。Eli Bendersky对Python中的方法有很好的概述。

如果您更改

start += end

它应该可以工作(在此处使用
xrange
使其更可见):


但是如果as@Satoru.Logic猜测你想要一个加权随机数,有更好的方法。Eli Bendersky对Python中的方法有很好的概述。

从Python 3.3.0文档中自豪地窃取:

-包含加权分布算法。
-包含累加算法

下面的代码是为2.X编写的:

import random
import bisect

D = {'a':0.66,'b':0.07,'c':0.04,'d':0.20,'e':0.03}

# This function is in Python 3.2+ itertools module.
def accumulate(iterable):
    'Return running totals'
    # accumulate([1,2,3,4,5]) --> 1 3 6 10 15
    it = iter(iterable)
    total = next(it)
    yield total
    for element in it:
        total = total + element
        yield total

# Extract the weights and build a cumulative distribution.
choices, weights = zip(*D.items())
cumdist = list(accumulate(weights))

# Make 1000 random selections
L = [choices[bisect.bisect(cumdist, random.random() * cumdist[-1])]
     for _ in xrange(1000)]

# Display the results
for c in sorted(D.keys()):
    print '{} {:3d}'.format(c,L.count(c))
输出:

a 652
b  72
c  43
d 200
e  33

从Python 3.3.0文档中骄傲地窃取:

-包含加权分布算法。
-包含累加算法

下面的代码是为2.X编写的:

import random
import bisect

D = {'a':0.66,'b':0.07,'c':0.04,'d':0.20,'e':0.03}

# This function is in Python 3.2+ itertools module.
def accumulate(iterable):
    'Return running totals'
    # accumulate([1,2,3,4,5]) --> 1 3 6 10 15
    it = iter(iterable)
    total = next(it)
    yield total
    for element in it:
        total = total + element
        yield total

# Extract the weights and build a cumulative distribution.
choices, weights = zip(*D.items())
cumdist = list(accumulate(weights))

# Make 1000 random selections
L = [choices[bisect.bisect(cumdist, random.random() * cumdist[-1])]
     for _ in xrange(1000)]

# Display the results
for c in sorted(D.keys()):
    print '{} {:3d}'.format(c,L.count(c))
输出:

a 652
b  72
c  43
d 200
e  33

您正在尝试实现加权随机算法,对吗?在最后一行中,它应该是
start=end
而不是
start+=end
@yakxxx我已经尝试过了,但是循环没有超过两次迭代,我无法找出原因..您正在尝试实现加权随机算法,对吗?在最后一行中,它应该是
start=end
而不是
start+=end
@yakxxx,我已经尝试过了,但是循环没有超过两次迭代,我无法找出原因..FWIW您没有提到为什么需要将
start+=end
更改为
start=end
,所以请允许我。这是因为,为了正确起见,前者需要是
start+=end start
,这当然可以简化为后者。我喜欢使用xrange(为了清楚地说明),并且链接到Eli Bendersky的页面对我非常有用。谢谢。FWIW您没有提到为什么需要将
start+=end
更改为
start=end
,请允许我。这是因为,为了正确起见,前者需要是
start+=end start
,这当然可以简化为后者。我喜欢使用xrange(为了清楚地说明),并且链接到Eli Bendersky的页面对我非常有用。谢谢