首页 > 解决方案 > Why my python program returning different Output on every execution

问题描述

I have been doing a challenge to find a runner up score from a list
or
You can say I was trying to find the second largest from a list.

array = '57 57 -57 57'

print(list(set(sorted(array.split(' '))))[-2])

But on every execution the program returning different Output.

Why it is behaving like this??

标签: pythonpython-3.xlistlist-manipulation

解决方案


Change it to this:

array = '57 57 -57 57'

print(list(sorted(set(array.split(' '))))[-2])

'set' doesn't preserve the order, so first get the set value and then sort it.


推荐阅读