【发布时间】:2021-10-08 17:02:31
【问题描述】:
def checkio(array: list) -> int:
"""
Sums even-indexes elements and multiply at the last
"""
a = []
if array:
for i in array:
if array.index(i) == 0 or array.index(i) % 2 == 0:
a.append(i)
print(a)
return sum(a) * array[-1]
else:
return 0
checkio([-37, -36, -19, -99, 29, 20, 3, -7, -64, 84, 36, 62, 26, -76, 55, -24, 84, 49, -65, 41])
print(a) 的输出为 [-37, -19, 29, 3, -64, 36, 26, 55, -65],但列表中不包含 84。为什么会这样?
【问题讨论】:
-
为什么84要列入列表?
-
array.index(i)将返回第一个匹配元素的索引,因为您有两个84但对于这两个它都会返回第一个84的索引,这是 9 奇数 -
为什么它没有按预期工作:Index of duplicates items in a python list
-
我不认为你使用
index()很正确。s.index(x)返回“s中x的第一次 出现的索引:docs.python.org/3/library/stdtypes.html#typesseq-common 请注意,“84”在您的列表中出现不止一次。