我无法添加给定列表

时间:2019-12-12 10:49:46

标签: python python-3.x

**无法添加给定列表a

a = ['1','2','3','4','5','6']


    def sums (a):
        return sum(a)

    print(sums(a))

给我错误unsupported operand type(s) for +: 'int' and 'str'

我知道我无法在不将str列表转换为int的情况下隐式添加a的情况,但是当我尝试将列表int转换为a = ['1','2','3','4','5','6'] def sums (a): int_a = int(a) return sum(a) print(sums(a))

int() argument must be a string, a bytes-like object or a number, not 'list'

它仍然给我错误**

SELECT ta.id, ta.val, tb.bval 
 FROM table_a ta
 LEFT JOIN table_b tb ON ta.id = tb.AID
 GROUP BY ta.id, ta.val, tb.bval
 HAVING tb.BVAL = 'B1' OR tb.BVAL IS NULL

只是一个学习者, 任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

这样做的时候

int_a = int(a)

代码尝试将列表a转换为int。您需要做的是

def sums (a):
    a = [int(x) for x in a]
    return sum(a)

这会将列表中的每个元素转换为整数。您可以将其进一步缩减为

def sums (a):
    return sum(int(x) for x in a)