Python:从列表

时间:2016-06-30 15:15:46

标签: python list-comprehension find-occurrences cdf

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

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)]

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

2 个答案:

答案 0 :(得分:1)

你的内心清单理解是关闭的。 有两个问题:

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

  2. 您在两次迭代中使用相同的变量名称。那是 容易混淆和容易出错。

  3. 请改为尝试:

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

答案 1 :(得分:1)

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)