Python 12.purify

define a function called purify that takes a list of numbers,removes all odd numbers in the list ,and returns the result.
DO not directly modify the list you are given as input,instead,return a new list with only the even numbers.

def purity(n):
even = []
for x in n:
if x % 2 == 0:
even.append(x)
return even

def purify(n):
return [x for x in n if x % 2 == 0]

最后一句话点明要定义一个新列表,看到了,而没有入脑。被odd numbers 占领认知资源,其实是even numbers.

你可能感兴趣的:(Python 12.purify)