简体   繁体   English

在计算大于和小于列表中某些值的值的数量时会跳过一些数字

[英]Some numbers are skipped in counting number of values greater and less than certain values in a list

I'm having problems with counting the number of values greater than start (initially 0) and less than end (initially 10) certain values in a list.我在计算列表中大于start (最初为 0)和小于end (最初为 10)某些值的值的数量时遇到问题。

The list contains 12 500 float numbers from the range 0 - 110. I want to count how many numbers out of the list are in the range 0-10, then 10-20, 20-30, (...), 100-110该列表包含 0-110 范围内的 12 500 个浮点数。我想计算列表中有多少数字在 0-10 范围内,然后是 10-20、20-30、(...)、100- 110

I have implemented a for loop with a list comprehension inside like that:我已经实现了一个带有列表理解的 for 循环,如下所示:

all_nums = [] # contains 12_500 float numbers

nums_counted = []
start = 0
end = 10
for x in range(11):
    count = len([x for x in to_int if start < x < end])
    nums_counted.append(count)
    start = start + 10
    end = end + 10

The problem is len(all_nums) = 12500 and sum(nums_counted) = 12492 , which should be equal to 12 500 as well.问题是len(all_nums) = 12500sum(nums_counted) = 12492 ,它们也应该等于 12 500。

I do not know what am I doing wrong and where those 8 numbers disappear我不知道我做错了什么以及那 8 个数字在哪里消失了

You can just do int division by 10您可以将 int 除以 10


> from collections import Counter
> 
> l = [0.0, 0.1, 6.1, 11.4, 19.2, 20.9, 40.1]
> 
> d = Counter()
> 
> for val in l:
>     binn = val//10
>     d[binn] += 1
> 
> print(l) 
> print(d.items())
[0.0, 0.1, 6.1, 11.4, 19.2, 20.9, 40.1]
dict_items([(0.0, 3), (1.0, 2), (2.0, 1), (4.0, 1)])

In this case 0.0 --> 0-10 range which has 3 count在这种情况下 0.0 --> 0-10 范围内有 3 个计数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM