简体   繁体   English

过滤掉特定条款

[英]Filtering out specific terms

I have written a function that uses derivative product rule to find derivative of a term: 我编写了一个函数,它使用衍生产品规则来查找术语的派生词:

def find_term_derivative(term):
    x , y = term
    new_term = (y*x, y-1)
    return new_term

def find_derivative(function_terms):
    new_function = []
    for term in function_terms:

        new_term = find_term_derivative(term)
        new_function.append(new_term)

        filtered_terms = filter(zero_filter, new_term)

find_derivative[(4, 3), (-3, 1)]

Ouputs [(12, 2), (-3, 0)] 输出[(12, 2), (-3, 0)] 12,2 [(12, 2), (-3, 0)] 3,0 [(12, 2), (-3, 0)]

However I want to use the filter function to remove any terms which output begin with zero. 但是我想使用过滤器函数来删除任何以零开头输出的术语。

For example Inputs [(3, 2), (-11, 0)] Currently Outputs [(6, 1), (0, -1)] 例如输入[(3, 2), (-11, 0)]当前输出[(6, 1), (0, -1)] 6,1 [(6, 1), (0, -1)]

However I want to filter & remove the second term because it begins with 0, removing it from the dictionary new_function 但是我想过滤和删除第二个术语,因为它以0开头,将其从字典new_function中删除

I am trying to define a filter function which analyses the first term of each tuple and checks if its 0, and removes it if is. 我试图定义一个过滤函数,它分析每个元组的第一项并检查它是否为0,如果是,则将其删除。 Is this how the filter function is used? 这是过滤功能的用法吗?

def zero_filter(variable):
    if new_term [0] = 0:
        return False 
    else:
        return True

Rather than writing a function that flags if a term should be filtered, just end your routine with a list comprehension that does the filtering. 而不是编写一个标记是否应该过滤术语的函数,而只需使用执行过滤的列表推导来结束您的例程。

filtered_list = [v for v in unfiltered_list if v[0]]

Your code has some other problems, but since you did not ask about them I'll not go into that. 你的代码有一些其他的问题,但既然你没有问过它们,我就不会讨论它。 Change the variables in the line I showed you to fit into your routine. 更改我显示的行中的变量以适合您的例程。


If you need Python's filter function , you could use this. 如果你需要Python的filter功能 ,你可以使用它。

def find_term_derivative(term):
    x , y = term
    new_term = (y*x, y-1)
    return new_term

def find_derivative(function_terms):
    new_function = []
    for term in function_terms:
        new_term = find_term_derivative(term)
        new_function.append(new_term)
    return list(filter(zero_filter, new_function))

def zero_filter(atuple):
    """Note if the first element of a tuple is non-zero
    or any truthy value."""
    return bool(atuple[0])

print(find_derivative([(4, 3), (-3, 1)]))
print(find_derivative([(3, 2), (-11, 0)]))

The printout from that is what you want: 打印输出就是你想要的:

[(12, 2), (-3, 0)]
[(6, 1)]

I did the non-zero check in the filter using the pythonic way: rather than check explicitly that the value is not zero, as in atuple[0] != 0 , I just checked the "truthiness" of the value with bool(atuple[0]) . 我使用pythonic方式在过滤器中进行了非零检查:而不是明确检查该值不为零,如atuple[0] != 0 ,我刚用bool(atuple[0])检查了值的“真实性” bool(atuple[0]) This means that values of None or [] or {} or () will also be filtered out. 这意味着也将过滤掉None[]{}() This is irrelevant in your case. 这与您的情况无关。

By the way, I used the name zero_filter since you did, but that does not seem to me to be the best name. 顺便说一句,我使用了名称zero_filter ,但是在我看来这不是最好的名字。 Perhaps filter_out_zeros would be better. 也许filter_out_zeros会更好。

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

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