给出一个总和a中所有数字的方法list.该方法应该能够跳过不是数字的元素.所以,sum([1, 2, 3])应该6但sum(['A', 1, 'B', 2, 3]) 也应该如此6.我怎么能做到这一点?
到目前为止我已经尝试过的内容:
def foo(list):
dict = "ABCDEFGHIJKLMN"
n = 0
for i in range(0, len(list) - 1):
if list[i].str in dict:
""
else:
n= n + list[i]
return n
print foo([1, 2, 3, 4, 5, 6, "A", "B"])
Run Code Online (Sandbox Code Playgroud)