简体   繁体   English

过滤掉特定的衍生物

[英]filtering out specific derivatives

I have generated several functions. 我已经生成了几个函数。

find_derivative simply takes the derivative of several terms using the product rule and makes a new equation. find_derivative使用产品规则简单地获取几个术语的导数并创建一个新的等式。

So if i input 3x^2 + 2x^3 所以,如果我输入3x ^ 2 + 2x ^ 3

I get 6x + 6x^2 我得到6x + 6x ^ 2

in the output [(6,1),(6,2)] 在输出[(6,1),(6,2)]

However I want to filter out any time I end up with an output with a tuple that begins with 0 但是,我希望在我最终得到一个以0开头的元组的输出时过滤掉

def find_derivative(function_terms):
    new_function = []
    for term in function_terms:
        new_term = find_term_derivative(term)
        new_function.append(new_term)
        filtered_list = list(filter(lambda x: x != 0, new_function))
    return filtered_list

The expected result from input [(3, 2), (-11, 0)] should be [(6, 1)] but this code is not removing the second term, [0,-1] 输入[(3,2),( - 11,0)]的预期结果应为[(6,1)],但此代码不会删除第二项,[0,-1]

EDIT: derivative function 编辑:衍生功能

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

You have the right idea with the filter, but take a look at the lambda you've written. 你对过滤器有正确的想法,但看看你写的lambda。 You're filtering on the condition lambda x : x != 0 , which will check if the element in the list is not zero. 你正在过滤条件lambda x : x != 0 ,它将检查列表中的元素是否不为零。

But the elements in your list are tuples, and you want to check if only the first number in the tuple is zero, so you need to update your condition to be lambda x : x[0] != 0 . 但是列表中的元素是元组,并且您想要检查元组中的第一个数字是否为零,因此您需要将条件更新为lambda x : x[0] != 0

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

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