如何修复下面的代码?

wes*_*ley 2 python python-2.7

def CostFunction(A):
    return sum(A)

A = [[1,1,1],[2,2,2]]
print CostFunction(A[0:])
Run Code Online (Sandbox Code Playgroud)

答案应该是3,但应该有一些问题A.

ars*_*jii 5

A[0:]是一个切片,而不是一个元素:

>>> A = [[1,1,1],[2,2,2]]
>>> A[0:]
[[1, 1, 1], [2, 2, 2]]
Run Code Online (Sandbox Code Playgroud)

更一般地,A[n:]A索引的元素返回n到列表的末尾.有关更多详细信息,请参阅Python的切片表示法.

我相信你想A[0]:

>>> A[0]
[1, 1, 1]
Run Code Online (Sandbox Code Playgroud)