简体   繁体   English

函数返回None而不是index

[英]function returns None instead of index

This code should return index of element, which is surrounded with equal sums of elements 此代码应返回元素的索引,该索引用相等的元素之和包围

def find_even_index(arr):
    for elem in arr:
        i = arr.index(elem)
        if sum(arr[:i]) == sum(arr[i+1:]):
            return i
        else:
            pass
print find_even_index([1,2,3,4,3,2,1])
print find_even_index([20,10,30,10,10,15,35])

and it returns 它返回

3 3

None 没有

instead of None should return 3 but it doesn't. 而不是None应该返回3,但不是。 Why?? 为什么??

Your problem is arr.index(elem) returns only the first index of elem in list arr . 您的问题是arr.index(elem)仅返回列表arrelem的第一个索引。

To demonstrate: 展示:

>>> lst = [1, 2, 3, 1, 2]                                   
>>> lst.index(1)                                            
0                                                           
>>> lst.index(2)                                            
1                                                           
>>> lst.index(3)                                            
2                                           

Use enumerate instead to keep track of index while iterating through list, like so: 在遍历列表时,请使用enumerate来跟踪索引,如下所示:

def find_even_index(arr):
    for i, _ in enumerate(arr):
        if sum(arr[:i]) == sum(arr[i+1:]):
            return i

print(find_even_index([1,2,3,4,3,2,1]))  # 3
print(find_even_index([20,10,30,10,10,15,35]))  # 3

try this out. 试试这个。 it tends to work, just that I am using recursion instead of looping 它倾向于工作,只是我使用递归而不是循环

    def func(array, i):
        if sum(array[:i]) == sum(array[i+1:]):
            return i
        else:
            a = i + 1
            return func(array, a)

    print(func([20,10,30,10,10,15,35], 0)) #produces 3 as the answer

The problem with the second case is when you get to the fourth iteration and your script executes i = arr.index(10) . 第二种情况的问题是,当您进行第四次迭代并且脚本执行i = arr.index(10) You expect this to return 3 , but list.index() finds the first instance of 10 in the list, which is in position 1. In this case, you don't actually want to iterate over the elements of the list, but over their indices. 您希望它返回3 ,但是list.index()在列表中找到列表10第一个实例,它位于位置1。在这种情况下,您实际上并不想要遍历列表的元素 ,而是遍历他们的索引。 Try this: 尝试这个:

def find_even_index(arr):
    for i in range(len(arr)):
        if sum(arr[:i]) == sum(arr[i+1:]):
            return i

The problem in your code is that arr.index(elem) always returns 1 (first index of elem in arr ) if elem equals 10, in your second call of find_even_index() . 您的代码中的问题是,在第二次调用find_even_index() ,如果elem等于10,则arr.index(elem)始终返回1( arrelem第一个索引find_even_index() So it can't work. 所以它不能工作。

I suggest you the following solution, iterating directly on the indexes instead of on the values of the list: 我建议您采用以下解决方案,直接在索引上而不是在列表的值上进行迭代:

def find_even_index(arr):
    for i in range(len(arr)):
        if sum(arr[0:i]) == sum(arr[i+1:]):
            return i
    return None

print(find_even_index([1,2,3,4,3,2,1]))          # 3
print(find_even_index([20,10,30,10,10,15,35]))   # 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM