3

我现在正在攻读计算机科学的期末考试,但我根本不明白这是如何工作的:

L = [ 8、6、7、5、3、0、9]

30. L[L[3]] - 1 是?
(一) * -1
(B) 错误
(三) 8
(D) 4
(五) 7

答案是-1..所以为了测试它是如何工作的,我刚刚做了 L[L[3]],答案是 0,然后我做了 L[L[4]] 并且 thar 等于 5,然后我做了 L[L [1]] 给出了 9 但如果我去 L[L[2]] 我得到一个列表索引超出范围错误。我在这里很困惑有人可以帮忙吗?

4

7 回答 7

11

L[3]5L[L[3]]是,L[5]00 - 1-1

于 2012-04-15T19:51:41.540 回答
4

These sort of problems are best worked one step at at time, from the inside out.

So, L[3] gives you 5. Use this value (5) as the index into the list again, i.e., L[5] and this gives you 0. Finally, 0 - 1 = -1, your answer.

于 2012-04-15T19:53:34.053 回答
2

这类似于使用数学函数(例如 f(g(x)))的函数组合。

我们取出列表并从内到外工作,所以我们得到L[L[3]] -> L[5] -> 0,然后从中减去一个得到-1

当你采取下一个例子时,L[L[1]] = L[6] -> 9. 最后,L[L[2]] = L[7] -> IndexError.

这一切都是关于从内到外评估复合索引(或函数,就此而言)。

于 2012-04-15T20:24:39.183 回答
0

以下是按步骤分解的答案:

L = [ 8、6、7、5、3、0、9]

L[L[3]] - 1 = -1

L[3] = 5,所以 L[L[3]] = L[5] = 0

于 2012-04-15T20:56:15.763 回答
0

Let's walk through the answer by showing the intermediate results, even though they are not displayed:

L[3] is 5, L[L[3]] is therefore L[5] = 0, and L[L[3]] -1 = 0-1 = -1

于 2012-04-15T19:52:24.150 回答
0

L[2] = 7 and the list only has only 7 elements, that is indice 0..6. So accessing L[L[2]] = L[7] is out of range...

于 2012-04-15T19:52:26.367 回答
0

it says, get the number at l[3] which is 5, then use that, so get the number at l[5] which is 0, then subtract 1.... ta dah!

于 2012-04-15T19:52:28.003 回答