简体   繁体   English

如果超过某个值,则计算元素

[英]Count elements if over a certain value

I have a list of elements with certain values of type float. 我有一个元素列表,其中包含某些float类型的值。 I want to iterate over the elements and count them if they are over a certain value, but also only count them if they appear over the treshold value a minimum_count of times. 我想迭代元素并在它们超过某个值时对它们进行计数,但是如果它们在阈值值上显示minimum_count次,则只计算它们。 So for example, if a have following input: 例如,如果a有以下输入:

list_of_values = [2.0, 2.0, 2.0, 2.0, 0, 0, 2.0, 2.0, 2.0, 0, 0]
treshold_value = 1.0
minimum_count = 4

the answer should be 4, since the treshold_value 1.0 is consecutively exceeded 4 times only at indexes 0-3. 答案应该是4,因为treshold_value 1.0仅在索引0-3处连续超过4次。 I now have the code below, 我现在有下面的代码,

for value in list_of_values:
    if value >= treshold_value:
        counter += 1
    if counter >= (minimum_count):
        time_use += 1
    if value < min_treshold_value:
        counter = 0
print(time_use)

I know there should be some pythonic way to achieve this :) 我知道应该有一些pythonic方式来实现这个:)

Edit: The sum of all consecutive subsequence values over the threshold should be counted. 编辑:应计算超过阈值的所有连续子序列值的总和。

The following use of groupby with a conditional generator and max with appropriate key function should work: 下面使用带有条件生成器的groupby和带有适当键函数的max应该可以工作:

from itertools import groupby

len(max((list(g) for k, g in groupby(list_ov, key=lambda x: x > threshold) if k), key=len))

groupby groups an iterable by consecutive identical values wrt to the key function. groupby通过连续相同的值wrt对key函数进行迭代。 It produces pairs of the key value and according sub-iterable. 它产生密钥值对和子迭代。

You could use itertools.groupby() to help: 您可以使用itertools.groupby()来帮助:

from itertools import groupby

def count_runs(list_of_values, threshold_value=1.0, minimum_count=4):
    count = 0
    for k, g in groupby(list_of_values, key=lambda x: x >= threshold_value):
        if k:
            g = list(g)
            if len(g) >= minimum_count:
                count += len(g)
    return count

>>> count_runs([2.0, 2.0, 2.0, 0.0, 0, 0, 2.0, 2.0, 2.0, 0, 0])
0
>>> count_runs([2.0, 2.0, 2.0, 2.0, 0, 0, 2.0, 2.0, 2.0, 0, 0])
4
>>> count_runs([2.0, 2.0, 2.0, 2.0, 0, 0, 3.0, 2.0, 2.0, 2.0, 10.0, 0, 0])
9

This will provide the count of the number of values that are above the threshold in groups of minimum_count or more. 这将提供minimum_count或更多组中高于阈值的值的数量。 Note that it handles multiple groups that match the criteria. 请注意,它处理符合条件的多个组。

For example the groupby() for the last example will return the following: 例如,最后一个示例的groupby()将返回以下内容:

>>> list_of_values = [2.0, 2.0, 2.0, 2.0, 0, 0, 3.0, 2.0, 2.0, 2.0, 10.0, 0, 0]
>>> for k, g in groupby(list_of_values, key=lambda x: x >= threshold_value):
...     print(k, list(g))
... 
True [2.0, 2.0, 2.0, 2.0]
False [0, 0]
True [3.0, 2.0, 2.0, 2.0, 10.0]
False [0, 0]

Any group of 1 or more values >= the threshold will appear in a group with key True . 任何一个或多个值> =阈值的组将出现在组为True的组中。 Only those with a length >= the minimum count will be considered further, where its length will be tallied with other such groups. 只有长度> =最小计数的那些将被进一步考虑,其长度将与其他此类组合计算。

This code can be written more succinctly, and far less readably, like this: 这段代码可以更简洁地编写,而且可读性更低,如下所示:

def count_runs(list_of_values, threshold_value=1.0, minimum_count=4):
    return sum(count for count in (len(list(g)) for k, g in groupby(list_of_values, key=lambda x: x >= threshold_value) if k) if count >= minimum_count)

just iterate over the list and create a dictionary with key = the float number and value = the number of times you encounter this number. 只需遍历列表并创建一个字典,其中key =浮点数和值=您遇到此数字的次数。 and only add to dict floats that are greater then threshold . 并且只添加大于阈值的dict浮点数。 something like this: 这样的事情:

d = {}
for f in   list_of_values :
    if f > treshold:
        if d.get(f,False):
             d[f] +=1
        else:
             d[f] = 1
max = 0
for k,v in d.iteritems():
    if v> max:
        max = v

return max

It looks like you don't care about the order. 看起来你不关心订单。 In this case, groupby isn't correct because it only groups adjacent elements. 在这种情况下, groupby不正确,因为它只对相邻元素进行分组。

You could use a Counter and two list comprehensions to filter values: 您可以使用计数器和两个列表推导来过滤值:

list_of_values = [2.0, 2.0, 2.0, 2.0, 0, 0, 3.0, 2.0, 2.0, 2.0, 10.0, 0, 0]
threshold_value = 1.0
minimum_count = 4

from collections import Counter
counter = Counter([x for x in list_of_values if x > threshold_value])
print(counter)
# Counter({2.0: 7, 3.0: 1, 10.0: 1})
print([(x, count) for x, count in counter.items() if count > minimum_count])
# [(2.0, 7)]

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

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