-2

I have the following list, where the 1st element is a generic value and the second is the number of occurrences of that value:

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

and I want to compute the CCDF (Complementary Cumulative Distribution Function) of those values appearing as second element of each tuple.

My code:

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

But this is not working as the outcome is 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)]

The sum of values in the second position in each tuple is 100. So, I would like to know how many times I have a value >= 2 (100-44=56), how many times I have a value >= 3 (100-44-21=35), and so forth. The result would thus be:

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

What is wrong in my list comprehension?

FaCoffee
  • 7,609
  • 28
  • 99
  • 174

2 Answers2

1

Your inner list comprehension is off. There are two issues:

  1. The correct syntax for a conditional (list) comprehension is: [x for x in someiterable if predicate(x)]

  2. You are using the same variable names in both iterations. That is confusing and error prone.

Try this instead:

ccdf=[(i,sum(k2 for i2,k2 in mylist if i2 >= i)) for i,k in mylist]
PeterE
  • 5,715
  • 5
  • 29
  • 51
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)
Timur Ridjanovic
  • 1,002
  • 1
  • 12
  • 18