简体   繁体   English

删除号码列表中的最后一个偶数

[英]Deleting the last even number in a number list

# Task_5
def delete_last_even(num_list):
    """Removes the last even number from a list of numbers.
    Args:
        num_list (list): List of numbers to be checked.
    Returns:
        list: List of numbers with the last even number
            removed.
    """
    if len(num_list) % 2 == 0:
        num_list.pop()
    else:
        return num_list

delete_last_even([7, 65, 1337, 8, -2, 24, 6, 67, 54, 36, 25, 1, 42, 9, 138, 4356, 6])

Let me know what I am interpreting wrong.让我知道我解释错了什么。 My if statement is asking if a number in variable (num_list) is even, then invoke the.pop() function to remove the last item from the list.我的 if 语句询问变量 (num_list) 中的数字是否为偶数,然后调用 the.pop() function 从列表中删除最后一项。 Finally, return num_list.最后,返回 num_list。 When I run this I get, None.当我运行它时,我得到,无。

First you have to find the index of the last even character.首先,您必须找到最后一个偶数字符的索引。 For that you can enumerate the reversed list num_list[::-1] .为此,您可以枚举反向列表num_list[::-1] Then you have to pop it from the list and you can either return the list or print the updated list as below.然后你必须从列表中弹出它,你可以返回列表或打印更新的列表,如下所示。 Note that here we are enumerating in order to find the index of the even letter inside the list.请注意,我们在这里进行枚举是为了找到列表中偶数字母的索引。

def delete_last_even(num_list):
    for index, number in enumerate(num_list[::-1]):
        if number % 2 == 0 :
            num_list.pop(len(num_list)-1-index)
            return num_list
    return num_list

You can check the output for your test case as shown below您可以检查 output 以获取您的测试用例,如下所示

print(delete_last_even([7, 65, 1337, 8, -2, 24, 6, 67, 54, 36, 25, 1, 42, 9, 138, 4356, 6]))

If you want to know more about enumerate function you can refer Documentation如果您想了解更多关于 enumerate function 的信息,您可以参考文档

It seems that you have misread the requirement, it asks you to remove last even number , but you've checked the len of the list.看来您误读了要求,它要求您删除last even number ,但您已经检查了列表的len And the earlier PO in comments have answered your why None return question already.之前评论中的 PO 已经回答了你为什么None返回问题。

Here is just one another way to approach it, and also compare the performance with other PO to show the difference.这只是另一种接近它的方法,并将性能与其他 PO 进行比较以显示差异。

def delete_last_even(nums):
    for i, num in enumerate(reversed(nums)):  # to reverse the contents of a list object in-place.  You won't be creating a new list. 
        if num % 2 == 0:
            nums.pop(~i)     # directly remove the num by ~i, aka, backward idx
            
            break

    return nums
    

Output: Output:

A = [1, 2, 3, 5, 6, 7]
delete_last_even(A) 
# [1, 2, 3, 5, 7]
delete_last_even([6, 1, 2, 3, 5, 6, 7])
#  [6, 1, 2, 3, 5, 7]            *

Performance comparison: first function is 51% faster than 2nd one.性能比较: first function is 51% faster than 2nd one. . .

If the input size is significant, it may be a legit concern.如果输入大小很大,这可能是一个合理的问题。


In [1]: nums = [1, 2, 3, 5, 7, 9, 11, 13, 15, 17, 21]

In [2]: def delete_last_even(nums):
   ...:     # same as theabove code
   ...:

In [3]: def delete_last_even2(nums):
   ...:     for idx, n in enumerate(nums[::-1]):
   ...:         if n % 2 == 0:
   ...:             nums.pop(idx)
   ...:             break
   ...:     return nums
   ...:

In [4]: %timeit delete_last_even(nums)
761 ns ± 54.1 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [5]: %timeit delete_last_even2(nums)
1.2 µs ± 145 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

Notice that when the length is divisible by 2, you have no objects you are returning.请注意,当长度可以被 2 整除时,您没有要返回的对象。

Additionally, what you have is removing the last element of the list if the length is even.此外,如果长度是偶数,您所拥有的是删除列表的最后一个元素。 From your task request it isn't clear to me that's what you want, as "remove last even number" could also mean find the last even element and remove it.根据您的任务请求,我不清楚您想要什么,因为“删除最后一个偶数”也可能意味着找到最后一个偶数元素并将其删除。 So [0,1,2] would map to [0,1], [2,1,3] would map to [1,3] and [3,1] would just map to itself if you are wanting to find the last even element in the list.因此,如果您要查找列表中的最后一个偶数元素。

  for index,num in enumerate(num_list[::-1]) :
    if num%2==0:
        num_list.pop(len(num_list)-1-index)
        break

return num_list

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

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