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.
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)