Python 第二多个元素是一个列表
我试图在列表中找到第二个max元素,我有一个重复案例的问题。为什么我的for循环没有删除列表Python 第二多个元素是一个列表,python,Python,我试图在列表中找到第二个max元素,我有一个重复案例的问题。为什么我的for循环没有删除列表1(a)中列表2(b)中max元素的所有实例。for循环应检查并删除所有重复实例。我在下面附上了我的代码 a=[] b=[] print "Enter the value of n\n" n= int(raw_input()) print "enter the values\n" for i in range(0,n): y = raw_input() a.append(int(y))
1(a)
中列表2(b)
中max元素的所有实例。for循环应检查并删除所有重复实例。我在下面附上了我的代码
a=[]
b=[]
print "Enter the value of n\n"
n= int(raw_input())
print "enter the values\n"
for i in range(0,n):
y = raw_input()
a.append(int(y))
max = a[0]
for i in a:
if max<i:
max=i
y=max
a.remove(y)
b.append(y)
for q in a:
for p in b:
if q==p:
a.remove(q)
max1 = a[0]
for i in a:
if max1<i:
max1=i
print max1
您的问题是在
列表上迭代,并且在这个循环中删除列表的元素。您不应该修改正在迭代的列表
您可以使用内置python函数:set
删除重复项,sorted
进行排序
>>> a = [32, 32, 32, 32, 11]
>>> sorted(set(a), reverse=True)[1]
11
而且,你知道Python 2很快就不再被维护了吗?如果可以,请更改为Python3 第k个元素的搜索是一个众所周知的问题,有一系列的解决方案可以在大约O(N)个时间内解决它,请阅读
Python的标准库(自2.4版起)包括heapq.nsmallest()
和nlargest()
,以O(n log k)时间返回排序列表
就个人而言,我宁愿使用线性O(N)算法来解决您的问题:
topmost = max( a ) # select the largest one
filtered = [i for i in a if i != topmost] # filter it out
second_best = max( filtered ) # here you have it!
下面的逻辑可能会有所帮助
a = []
print "Enter the value of n\n"
n= int(raw_input())
print "enter the values\n"
for i in range(0,n):
y = raw_input()
a.append(int(y))
# initialise max and second max to negative infinity
max = float("-inf")
second_max = float("-inf")
for i in a:
# if element is greater than max, set max to greater element and
# second max to previous max element.
if max < i:
second_max = max
max = i
# if the number is between max and second_max, update second_max
# only
elif max > i and second_max < i:
second_max = i
print second_max
代码的问题是,您正在迭代一个列表并从循环中的同一个列表中删除项,这将减少列表中的元素,并且无法按预期工作
问题的部分是
for q in a:
for p in b:
if q==p:
a.remove(q)
在这里,您正在迭代lista并删除循环块中的元素a.remove(q)
因此,在第一种情况下,32 32 11->在删除32时可以正常工作
但是在第二种情况下,它不会删除第三个32,并且您将在中得到[32,11],您可以通过使用max heapSee来提高运行时和空间效率a.remove(y)
仅删除第一个实例。您不应该同时迭代列表和删除列表中的项目。非常感谢。我有个小问题。列表b有max元素,for循环在列表上迭代,查找max值(即列表2的值)并删除它的所有实例。因此,内部循环将结束,但外部循环应检查列表1的所有实例?所以按照你们的说法,由于我们在删除和迭代列表的同时索引失败了?我理解对了吗?Thanksys是真的,假设您删除循环中列表中的第一项,第二项将成为第一个元素,并且由于该索引(第一个索引)已被覆盖,该元素将不会在下一次迭代中处理。现在明白了。非常感谢!这可以通过简单的方法来完成,b=list(set(a))b.remove(max(b))max(b)
非常感谢。。作为我的python新手,您能解释一下为什么这样做:filtered=[I for I in a if I!=topmost]而不是filtered=[I in a if I!=topmost]?因为a中的i应该足以转换整个列表?thanks@SarthakGoyal它不仅仅是遍历列表,它是列表理解——在原始列表的基础上创建另一个列表。基本上,您可以向后重写它,for i in a:if i!=最上面的:filtered.append(i)
——但这会更长,更难理解。在[
之后的第一个i
是进入新列表的值,不必是i
,可以是i*i
或exp(2.0,i)
或任何您喜欢的值。
a = []
print "Enter the value of n\n"
n= int(raw_input())
print "enter the values\n"
for i in range(0,n):
y = raw_input()
a.append(int(y))
# initialise max and second max to negative infinity
max = float("-inf")
second_max = float("-inf")
for i in a:
# if element is greater than max, set max to greater element and
# second max to previous max element.
if max < i:
second_max = max
max = i
# if the number is between max and second_max, update second_max
# only
elif max > i and second_max < i:
second_max = i
print second_max
print filter(lambda x: x != max, a)
for q in a:
for p in b:
if q==p:
a.remove(q)