嗨,我正在学习python,在一次练习中,他们问我一个函数,该函数接受任意数量的参数,并返回仅包含偶数个参数的列表。
我知道我的代码是错误的
def myfunc(*args):
for n in args:
if n%2 == 0:
return list(args)
myfunc(1,2,3,4,5,6,7,8,9,10)
答案 0 :(得分:0)
执行列表理解,从number_of_payments
中选择与我们的选择条件匹配的元素:
args
答案 1 :(得分:0)
这也可能会有所帮助,但是,前面的注释看起来更高级:
def myfunc(*args):
lista = []
for i in list(args):
if not i % 2:
lista.append(i)
return lista
答案 2 :(得分:0)
连拍
def myfunc(*args):
abc = []
for n in args:
if n%2==0:
abc.append(n)
return abc
答案 3 :(得分:0)
def myfunc(*args):
mylist = []
for x in list(args):
if x % 2 == 0:
mylist.remove(x)
return mylist
答案 4 :(得分:0)
def myfunc(*args):
even=[]
for n in args:
if n %2==0:
even.append(n)
else:
pass
return even
myfunc(1,2,3,4,8,9)
答案 5 :(得分:0)
def myfunc(*args):
#solution 1
# Create an empty list
mylist = []
for number in args:
if number %2 == 0:
mylist.append(number)
else:
pass
# return the list
return mylist
#solution 2
# Uses a list comprehension that includes the logic to find all evens and the list comprehension returns a list of those values
# return [n for n in args if n%2 == 0]
答案 6 :(得分:0)
您需要创建一个空列表来包含偶数。还将您的参数转换为列表。然后将偶数附加到新创建的列表中。
def myfunc(*args):
new_list = []
for num in list(args):
if num % 2 == 0:
new_list.append(num)
else:
pass
return new_list
答案 7 :(得分:-1)
def myfunc(*args):
x=[]
for i in list(args):
if i%2==0:
x.append(i)
return x