1

Given a dictionary, I need to calculate the sum of the logarithms of the values contained in the dictionary, until that sum is greater than 1.

I've started by calculating the partial sums:

r = [itertools.accumulate(math.log(items.values(),2))]

But I'm stuck on how to terminate the operation when the sum gets greater than 1.

4

1 回答 1

3

您可以尝试使用itertools.takewhile

takewhile(lambda x: x<1, accumulate(math.log(x,2) for x in items.values()))
于 2013-09-06T12:26:00.020 回答