**无法添加给定列表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
只是一个学习者, 任何帮助将不胜感激!
答案 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)