问题
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 numbers within the range from num_lst.
I've seen many posts and most are using numpy or pandas to solve it. However I want to find traditional ways to solve this without using np and pd. Can anyone give me the right direction.
num_range = [(0.0, 20.0), (20.0, 40.0), (40.0, 60.0), (60.0, 80.0), (80.0, 100.0)]
num_lst = [x for x in range(100)]
#Preferred output
frequency_dict ={(0.0, 20.0):20, (20.0, 40.0):20,
(40.0, 60.0):20,(60.0, 80.0):20,
(80.0, 100.0):20}
回答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.
来源:https://stackoverflow.com/questions/62995108/count-occurance-of-number-from-given-range