Skip to content Skip to sidebar Skip to footer

Count Occurance Of Number From Given Range

My objective is to count the frequency of number in num_lst to the range in num_range. And display the output to a dictionary where key is the range, value is the frequencies of nu

Solution 1:

if all the ranges use integers you can exploit set overlap, seta.intersection(setb) gives all elements in common between 2 sets, then the len of that is how many are in common:

num_range = [(0, 20), (20, 40), (40, 60), (60, 80), (80, 100)]
num_lst = set(range(100))

frequency_dict = {}
for a,b in num_range:
    frequency_dict[a,b] = len(num_lst.intersection(range(a,b)))

print(frequency_dict)

in more general, you can just use a nested loop over the range and see if it falls between each category:

num_range = [(0, 20), (20, 40), (40, 60), (60, 80), (80, 100)]
num_lst = range(100)

frequency_dict = dict.fromkeys(num_range, 0) # initial dictionary has 0 in all entries
for a,b in num_range:
    for i in num_lst:
        if a<=i<b:
            frequency_dict[a,b] += 1

print(frequency_dict)

or if you want it as a one liner with comprehensions:

frequency_dict = {(a,b):sum(a<=i<b for i in num_lst) for a,b in num_range}

And if this nested loop isn't fast enough for your liking, that is why so many people do it with numpy and pandas.

Post a Comment for "Count Occurance Of Number From Given Range"