Python 数组偶数索引元素求和算法 - 代码示例
"Python 数组偶数索引元素求和算法 - 代码示例"\n\n本篇文章介绍了使用 Python 语言计算数组中索引为偶数的元素之和的算法,并提供了代码示例和详细解释。\n\n假设 int 类型数组为 arr,可以使用以下方法求出索引是偶数的元素之和:\n\npython\ndef sum_of_even_index(arr):\n\ttotal = 0\n\tfor i in range(len(arr)):\n\t if i % 2 == 0:\n\t total += arr[i]\n\treturn total\n\n\n示例用法:\n\npython\narr = [1, 2, 3, 4, 5, 6]\nresult = sum_of_even_index(arr)\nprint(result) # 输出 9,因为 1+3+5=9\n
原文地址: https://www.cveoy.top/t/topic/pZ74 著作权归作者所有。请勿转载和采集!