简体   繁体   English

如何在不反转列表的情况下找到列表中最后一个奇数的索引?

[英]How to find the index of the last odd number in a list, without reversing the list?

Have this so far, and essentially want to get there is something wrong with the position of last_odd as the compiler says the pop index is out of range?到目前为止,如果编译器说 pop 索引超出范围,那么 last_odd 的last_odd有问题吗?

def remove_last_odd(numbers):
    has_odd = False
    last_odd = 0 
    for num in range(len(numbers)):
        if numbers[num] % 2 == 1:
            has_odd = True
            last_odd = numbers[num]
              
    if has_odd:
        numbers.pop(last_odd)
        
numbers = [1, 7, 2, 34, 8, 7, 2, 5, 14, 22, 93, 48, 76, 15, 6]

As @DeepSpace said, list.pop will正如@DeepSpace 所说, list.pop

Remove the item at the given position in the list, and return it.删除列表中给定 position 处的项目,并将其返回。 If no index is specified, a.pop() removes and returns the last item in the list.如果未指定索引,a.pop() 将删除并返回列表中的最后一项。

So basically, the solution to your problem would be to replace last_odd = numbers[num] with last_odd = num .所以基本上,您的问题的解决方案是将last_odd = numbers[num]替换为last_odd = num

list.pop() receive index as argument and remove the value. list.pop()接收索引作为参数并删除值。 So, last_odd should be assigned to num instead of numbers[num]所以, last_odd应该分配给num而不是numbers[num]

Your function doesn't have return value yet.您的 function 还没有返回值。 It should return numbers list.它应该返回numbers列表。

def remove_last_odd(numbers):
    for num in range(len(numbers)):
        if numbers[num] % 2 == 1:
            last_odd = num
    numbers.pop(last_odd)
    return numbers

numbers = [1, 7, 2, 34, 8, 7, 2, 5, 14, 22, 93, 48, 76, 15, 6]
print(remove_last_odd(numbers))

# [1, 7, 2, 34, 8, 7, 2, 5, 14, 22, 93, 48, 76, 6]

Or iterating numbers value and using remove() method instead of pop() :或者迭代numbers值并使用remove()方法而不是pop()

def remove_last_odd(numbers):
    for num in numbers:
        if num % 2: last_odd = num
    numbers.remove(last_odd)
    return numbers

numbers = [1, 7, 2, 34, 8, 7, 2, 5, 14, 22, 93, 48, 76, 15, 6]
print(remove_last_odd(numbers))

# [1, 7, 2, 34, 8, 7, 2, 5, 14, 22, 93, 48, 76, 6]

No need to reverse the list but you can search it in reverse.无需反转列表,但您可以反向搜索。

def remove_last_odd(numbers):
    for i in range(len(numbers)-1, -1, -1):
        if numbers[i] & 1:
            numbers.pop(i)
            break

numbers = [1, 7, 2, 34, 8, 7, 2, 5, 14, 22, 93, 48, 76, 15, 6]

remove_last_odd(numbers)

print(numbers)

Output: Output:

[1, 7, 2, 34, 8, 7, 2, 5, 14, 22, 93, 48, 76, 6]

Option:选项:

If you insist on a 'forward' search then:如果您坚持“向前”搜索,那么:

def remove_last_odd(numbers):
    ri = -1
    for i, v in enumerate(numbers):
        if v & 1:
            ri = i
    if ri >= 0:
        numbers.pop(ri)

To get the index of the last odd element you can use the reversed() iterator which will not reverse the original list object.要获取最后一个奇数元素的索引,您可以使用reversed()迭代器,它不会反转原始列表 object。

A quick way to get the index is:获取索引的快速方法是:

   >>> numbers = [1, 7, 2, 34, 8, 7, 2, 5, 14, 22, 93, 48, 76, 15, 6]
   >>> -next(filter(lambda v: v[1]%2==1, enumerate(reversed(numbers))))[0] 
   -1

Even for very large lists with a lot of even numbers at the end the result will be delivered quite quick (compared with the proposed code):即使对于最后有很多偶数的非常大的列表,结果也会很快交付(与建议的代码相比):

   >>> from timeit import timeit
   >>> def find_last_odd(numbers):
           for num in range(len(numbers)):
               if numbers[num] % 2 == 1:
                   last_odd = num
           return last_odd
   >>> numbers2=numbers+([2]*10000) # create a list with 10000 even numbers at the end!
   >>> timeit(lambda: find_last_odd(numbers2),number=100)
   0.5675344999999936
   >>> timeit.timeit(lambda: -next(filter(lambda v: v[1]%2==1, enumerate(reversed(numbers2)))).__getitem__(0),number=100)
   0.10892959999998197
    

Remove the item at the given position in the list, and return it.删除列表中给定 position 处的项目,并将其返回。 If no index is specified, a.pop() removes and returns the last item in the list.如果未指定索引,a.pop() 将删除并返回列表中的最后一项。

So basically, the solution to your problem would be to replace last_odd = numbers[num] with last_odd = num.所以基本上,您的问题的解决方案是将 last_odd = numbers[num] 替换为 last_odd = num。

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

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