Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何删除列表中的最后一个奇数_Python_List - Fatal编程技术网

Python 如何删除列表中的最后一个奇数

Python 如何删除列表中的最后一个奇数,python,list,Python,List,我有一个数字列表,我想删除其中最后一个奇数。只有在以下情况之前,列表中最后一个奇数没有重复时,此代码才能正常工作: numbers = [1, 7, 2, 34, 8, 7, 2, 5, 14, 22, 93, 48, 76, 15, 7] odd_numbers = [] def remove_last_odd(numbers): for n in numbers: if n % 2 != 0: odd_numbers.append(n)

我有一个数字列表,我想删除其中最后一个奇数。只有在以下情况之前,列表中最后一个奇数没有重复时,此代码才能正常工作:

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

def remove_last_odd(numbers):
    for n in numbers:
        if n % 2 != 0:
            odd_numbers.append(n)    
    numbers.remove(odd_numbers[-1])
    return numbers
因此,我没有删除最后7个,而是删除了“数字”列表中第一个出现的7


有人能帮忙吗?

这不是最有效的方法,但可以:

def remove_last_odd(numbers):
    rnumbers = numbers[::-1]
    for n in rnumbers:
        if n % 2 != 0:  
            rnumbers.remove(n)
            break
    return rnumbers[::-1]

基本上是这样做的:反转列表,删除第一个奇数,再次反转并返回。

这是因为循环从第一个元素迭代到最后一个元素。只需反向循环:

for n in reversed(numbers):

使用
pop()
而不是
remove()
remove()
在列表中搜索参数值并取出它找到的第一个参数值
pop()
删除作为参数传入的特定索引处的元素。您需要重构您的代码,以便找到最后一个奇数的索引,但是
pop()
将按照您的预期执行。

解决方案,而不必反转列表:

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

_numbers = numbers[::-1]

for i, index in enumerate(_numbers):
    if i % 2 != 0:
        del(_numbers[index])
    break
print _numbers
def remove_last(iterable, condition):
     result = []
     pre = []
     for x in iterable:
        if condition(x):
            result.extend(pre)
            pre = []
        pre.append(x)
    return result + pre[1:]

remove_last([1,5,6,7,8,9,10], lambda x: x&1)
>>> [1, 5, 6, 7, 8, 10]

您可以将
max
enumerate
一起使用,而无需反转列表。
max
提供奇数数字的最后一个索引。并且
pop
接受一个可选参数,该参数是要删除的元素的索引

def remove_last(numbers):
    numbers.pop(max(i for i, j in enumerate(numbers) if j % 2 != 0))
    return numbers

为了提高效率,这里有一个解决方案,它不会颠倒列表或重复两次

def remove_last_odd(numbers):
    idx = len(numbers)
    last_odd = False
    while not last_odd and idx > 0:
        idx -= 1
        last_odd = (numbers[idx] % 2 != 0)

    return numbers[0:idx]

这类似于内斯的方法,但我认为更清楚一点。首先,我反转列表,将每个元素添加到第一个列表中

然后我逐项检查第一个\u列表,使用布尔标志标识(并跳过)第一个奇数,将其他所有内容附加到第二个\u列表中

最后,我反转第二个_列表,将其元素添加到第三个_列表中。完成了

def remove_last_odd(numbers):
    found_it = False
    first_list = []
    second_list = []
    third_list = []

    for num in numbers[::-1]:
        first_list.append(num)

    for num in first_list:

        if found_it == False:

            if num % 2 == 0:
                second_list.append(num)
            else:
                found_it = True

        else:
            second_list.append(num)

    for num in second_list[::-1]:
        third_list.append(num)

    return third_list

使用For循环而不使用最后一个奇数的索引反转列表的解决方案

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 =num 

      if has_odd:
          numbers.pop(last_odd)

   return numbers

不完全是这样,这是因为remove()删除了第一个匹配项,而不是因为他从头到尾进行迭代。您的解决方案将具有相同(错误)的输出。请在列表中向后循环,而不是向前循环。。