小编use*_*487的帖子

仅汇总列表中包含的数字

给出一个总和a中所有数字的方法list.该方法应该能够跳过不是数字的元素.所以,sum([1, 2, 3])应该6sum(['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)

python list python-2.7

10
推荐指数
3
解决办法
3392
查看次数

标签 统计

list ×1

python ×1

python-2.7 ×1