计算返回的值不正确

时间:2014-06-11 14:43:20

标签: arrays for-loop python-3.x

此函数用于汇总列表的偶数索引中的所有数字,然后将此总和乘以列表的最后一个数字。

checkio = [-37,-36,-19,-99,29,20,3,-7,-64,84,36,62,26,-76,55,-24,84,49,-65,41]




def checkzi(array):
    if len(array) != 0:
        sum_array = 0
        for i in array:
            x = array.index(i)
            if (x % 2 == 0):
                sum_array += int(i)
                print (sum_array)
        print (sum_array)
        answer = (sum_array) * (array[len(array)-1])
        return (answer)
    else:
        return 0


checkzi(checkio)

我得到的'print'输出是: -37 -56 -27 -24 -88 -52 -26 29 -36 -36

通过这个我可以理解,正确添加的最后一个数字是55. 55之后,84没有正确添加。 更重要的是,我得到的最终总和是-1476,而假设它是1968年。

我找不到任何理由。不管怎样我都看不到。

任何人都知道吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

array.index()将始终返回找到值的第一个索引。所以你循环遍历每个元素,然后查看它所在的索引 - 但如果有重复的元素(有),那么你只看到第一个的索引,导致你总是添加(或每当你遇到它时,总是排除那个数字。

更简洁(更快)的方法是首先使用Python的slice notation迭代遍历列表中的偶数元素:

checkio = [-37,-36,-19,-99,29,20,3,-7,-64,84,36,62,26,-76,55,-24,84,49,-65,41]

def checkzi(array):
    sum_array = 0
    for value in array[::2]: #loop over all values at even indexes
        sum_array += value
    return sum_array * array[-1] # multiply by the last element in the original array

使用内置的sum功能,您甚至可以对这一切进行整理:

def checkzi(array):
    return sum(array[::2]) * array[-1]

答案 1 :(得分:0)

问题是array.index()将返回值的第一个实例。您的值为84两次 - 因此,由于第一个索引是奇数,因此您永远不会添加它。

您确实需要跟踪索引,而不是依赖于值的唯一性。你这样做

for idx, val in enumerate(array):

现在您的第一个值将是索引,第二个值将是该值。测试idx%2==0,你可以从这里看出来。

更新这里是完整的代码,明确(我希望)这是如何工作的:

checkio = [-37,-36,-19,-99,29,20,3,-7,-64,84,36,62,26,-76,55,-24,84,49,-65,41]

def checkzi(array):
    if len(array) != 0:
        sum_array = 0
        for idx, x in enumerate(array):
            print "testing element", idx, " which has value ", x
            if (idx % 2 == 0):
                sum_array += x
                print "sum is now ", sum_array
            else:
                print "odd element - not summing"
        print (sum_array)
        answer = (sum_array) * (array[len(array)-1])
        return (answer)
    else:
        return 0

checkzi(checkio)

输出:

testing element 0  which has value  -37
sum is now  -37
testing element 1  which has value  -36
odd element - not summing
testing element 2  which has value  -19
sum is now  -56
testing element 3  which has value  -99
odd element - not summing
testing element 4  which has value  29
sum is now  -27
testing element 5  which has value  20
odd element - not summing
testing element 6  which has value  3
sum is now  -24
testing element 7  which has value  -7
odd element - not summing
testing element 8  which has value  -64
sum is now  -88
testing element 9  which has value  84
odd element - not summing
testing element 10  which has value  36
sum is now  -52
testing element 11  which has value  62
odd element - not summing
testing element 12  which has value  26
sum is now  -26
testing element 13  which has value  -76
odd element - not summing
testing element 14  which has value  55
sum is now  29
testing element 15  which has value  -24
odd element - not summing
testing element 16  which has value  84
sum is now  113
testing element 17  which has value  49
odd element - not summing
testing element 18  which has value  -65
sum is now  48
testing element 19  which has value  41
odd element - not summing
48

你显然想把print语句拿出来 - 我添加它们来帮助解释程序流程。