简体   繁体   中英

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. Finally, return 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] . 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

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

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. And the earlier PO in comments have answered your why None return question already.

Here is just one another way to approach it, and also compare the performance with other PO to show the difference.

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:

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. .

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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