Python:从列表中构建 CCDF

Python: building a CCDF out of a list

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

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]

但这不起作用,因为结果无效:

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)