本文介绍了计算给定范围内数字的出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是将num_lst中数字的频率计入num_range中的范围.并将输出显示到字典中,其中key是范围,value是从num_lst范围内的数字的频率.

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.

我看过很多帖子,大多数都使用numpy或pandas来解决.但是,我想找到不使用np和pd的传统方法来解决此问题.谁能给我正确的方向.

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}

推荐答案

如果所有范围都使用整数,则可以利用集合重叠, seta.intersection(setb)给出2个集合之间所有相同的元素,则其中的 len 是有多少共同点:

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}

如果这个嵌套循环的速度不足以满足您的喜好,这就是为什么这么多人使用numpy和pandas来做到这一点.

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

这篇关于计算给定范围内数字的出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 06:03