【问题标题】:Bad value returned by calculation计算返回的错误值
【发布时间】:2014-06-11 14:43:20
【问题描述】:

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

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)

我得到的“打印”输出是: -37 -56 -27 -24 -88 -52 -26 29 -36 -36 .

由此我可以理解,最后正确添加的数字是 55。在 55 之后,没有正确添加 84。 更重要的是,我得到的最终总和是 -1476,而假设是 1968。

我找不到任何原因。反正我也看不到。

有人知道吗?

谢谢!!

【问题讨论】:

  • 你能修正一下缩进吗?正如当前显示的那样,else: 不在正确的位置。缩进显然对 Python 至关重要,所以我不想假设我知道你是如何缩进代码的。
  • 是的,很抱歉,我修好了

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


【解决方案1】:

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

一种更简洁(更快)的方法是首先只迭代列表的偶数元素,使用 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]

【讨论】:

  • 这是一个不错的紧凑型解决方案!
【解决方案2】:

问题是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 语句 - 我添加它们是为了帮助解释程序流程。

【讨论】:

  • 不,我不认为“for i in array”返回一个索引,这就是为什么我在声明之后添加:x = array.index(i) 更是如此,是的,我正在乘以最后一个元素,请阅读问题。
  • 感谢 Floris 的回复,但是如果我有一个 for 循环,它不会也遍历第二个 84 吗?怎么解决?
  • 查看解决方案的更新答案。它遍历这些值,但没有使用您的方法找到正确的索引。我建议的方法无需查找即可为您提供索引和值。
  • 对不起,我不明白这个说法:“for idx, val in enumerate(array)”。什么是 idx?而且我不明白这个功能,所以我不会弄明白......你能再解释一下吗?
  • 这没有任何好处..我添加这个后得到了一个完全错误的计数:for idx, val in enumerate(array): if (idx % 2 == 0): sum_array += int(idx) 打印 (sum_array)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-18
  • 1970-01-01
相关资源
最近更新 更多