Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.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 My.remove()不是';t即使我';我通过迭代器传递它。我能';我似乎找不到问题所在_Python_Python 3.x - Fatal编程技术网

Python My.remove()不是';t即使我';我通过迭代器传递它。我能';我似乎找不到问题所在

Python My.remove()不是';t即使我';我通过迭代器传递它。我能';我似乎找不到问题所在,python,python-3.x,Python,Python 3.x,我的代码应该返回第三高的不同数字。我的代码似乎无法相应地工作。对列表进行排序(以确保)。转换成一个集合。转换回列表,从右侧取第三个 def thirdMax(nums): highest = max(nums) for i in nums: if i == highest: nums.remove(i) print(nums) secondHighest = max(nums) for i in nums:

我的代码应该返回第三高的不同数字。我的代码似乎无法相应地工作。

对列表进行排序(以确保)。转换成一个集合。转换回列表,从右侧取第三个

def thirdMax(nums):

    highest = max(nums)

    for i in nums:
        if i == highest:
            nums.remove(i)
    print(nums)

    secondHighest = max(nums)

    for i in nums:
        if i == secondHighest:
            nums.remove(i)

    thirdHighest = max(nums)

    return thirdHighest

thirdMax([1,2,3,3,3,3,3,3,4,4,4,4,4,4,4])

您可以将其中一些步骤组合起来,但为了清晰起见,我将它们全部写了出来:)

哦,这是一种更简单的方法。但是为什么我的迭代删除方法不起作用呢?因为删除只删除它找到的值的第一个实例。在您的情况下,删除的2将达到删除的4,剩下的4仍然是最高的数字。
l = [1,2,3,3,3,3,3,3,4,4,4,4,4,4,4]
l.sort()
s = set(l)
l = list(s)
third = l[-3]