过滤掉特定术语 [英] 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)]
输出[(12, 2), (-3, 0)]
但是,我想使用过滤器功能删除所有以零开头的项.
However I want to use the filter function to remove any terms which output begin with zero.
例如
输入[(3, 2), (-11, 0)]
当前输出[(6, 1), (0, -1)]
For example
Inputs [(3, 2), (-11, 0)]
Currently Outputs [(6, 1), (0, -1)]
但是我要过滤&删除第二项,因为它以0开头,将其从字典new_function中删除
However I want to filter & remove the second term because it begins with 0, removing it from the dictionary new_function
我试图定义一个过滤器函数,该函数分析每个元组的第一项并检查其是否为0,如果为0,则将其删除.这是使用过滤功能的方式吗?
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. 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.
如果您需要Python的 filter
函数,则可以用这个.
If you need Python's filter
function, you could use this.
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)]))
您想要的打印输出是
[(12, 2), (-3, 0)]
[(6, 1)]
我使用pythonic方法在过滤器中进行了非零检查:不是像atuple[0] != 0
中那样显式检查该值是否为零,我只是使用bool(atuple[0])
检查了该值的真实性".这意味着None
或[]
或{}
或()
的值也将被过滤掉.这与您的情况无关.
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])
. This means that values of None
or []
or {}
or ()
will also be filtered out. This is irrelevant in your case.
顺便说一句,由于您使用过,所以我使用了zero_filter
这个名字,但是在我看来,这并不是最好的名字.也许filter_out_zeros
会更好.
By the way, I used the name zero_filter
since you did, but that does not seem to me to be the best name. Perhaps filter_out_zeros
would be better.
这篇关于过滤掉特定术语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!