Python:从列表中构建CCDF



我有以下列表,其中第一个元素是一个泛型值,第二个是该值出现的次数:

mylist=[(2, 45), (3, 21), (4, 12), (5, 7), 
(6, 2), (7, 2), (8, 3), (9, 2), 
(10, 1), (11, 1), (15, 1), (17, 2), (18, 1)]

,我想计算这些值作为每个元组的第二个元素的CCDF(互补累积分布函数)。

我代码:

ccdf=[(i,sum(k>=i for i in mylist)) for i,k in mylist]

但是这不起作用,因为结果是void:

ccdf=[(2, 0), (3, 0), (4, 0), (5, 0), 
(6, 0), (7, 0), (8, 0), (9, 0), 
(10, 0), (11, 0), (15, 0), (17, 0), (18, 0)]

每个元组中第二个位置的值之和为100。所以,我想知道有多少次我的值>= 2(100-44=56),有多少次我的值>= 3(100-44-21=35),等等。结果将是:

ccdf=[(2, 56), (3, 35), (4, 23), (5, 16), 
(6, 14), (7, 12), (8, 9), (9, 7), 
(10, 6), (11, 5), (15, 4), (17, 3), (18, 1)]

我的列表理解有什么问题?

您的内部列表理解已关闭。有两个问题:

  1. 条件(列表)推导式的正确语法是:[x for x in someiterable if predicate(x)]

  2. 在两个迭代中使用相同的变量名。这是

试试这个:

ccdf=[(i,sum(k2 for i2,k2 in mylist if i2 >= i)) for i,k in mylist]
mylist = [
    (2, 45), (3, 21), (4, 12), (5, 7), (6, 2), 
    (7, 2), (8, 3), (9, 2), (10, 1), (11, 1), 
    (15, 1), (17, 2), (18, 1)
]
def get_sum_of_values(_list):
    return reduce(lambda a, b: a + b[1], _list, 0)

def calculate_ccdf(mylist):
    sum_of_values = get_sum_of_values(mylist)
    return [(_tuple[0], sum_of_values - get_sum_of_values(mylist[0:index+1])) for index, _tuple in enumerate(mylist)]

print calculate_ccdf(mylist)

相关内容

  • 没有找到相关文章

最新更新