擅长:python、mysql、java
<p>请尝试以下代码:</p>
<pre><code>data = [(0.1,100), (0.13,300), (0.2,10)]
buckets = [((0,0.14), 2), ((0.135,0.19), 1), ((0.19,0.21), 2), ((0.19,0.24), 3)]
def foo(tpl): ## determine the buckets a data-tuple is enclosed by list of IDs
x, s = tpl
lst = []
for bucket in buckets:
rnge, iid = bucket
if x>rnge[0] and x<rnge[1]: lst.append(iid)
return lst
data = [[dt, foo(dt)] for dt in data]
scores_dict = {}
for tpl in data:
score = tpl[0][1]
for iid in tpl[1]:
if iid in scores_dict: scores_dict[iid]+=score
else: scores_dict[iid] =score
for key in scores_dict:
print key,":",scores_dict[key]
</code></pre>
<p>此代码段将导致:</p>
^{pr2}$
<p>如果没有打印任何bucket ID,则该bucket中没有X值或总和为零。在</p>