python - 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.
- On first execution it returned
57
- On Second execution it returned
-57
- On Third, Fourth, Fifth, Sixth, Seventh execution it returned
57
- Again on Eigth execution it again returned
-57
Why it is behaving like this??
解决方案
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.
推荐阅读
- matlab - 使用符号工具箱简化已知函数的表达式(例如三角函数)
- angular - 无法在 Angular 管道中将一个数组的值推入另一个数组
- c# - SlideMenuLayout 幻灯片不仅仅是布局,而是一个活动
- html - navbar 小于 navbar-brand
- python - 如何获取 lxml.etree._ElementTree 对象的父路径
- javascript - 在另一个组件上使用来自 ngFor 的信息
- python - 使用 scipy 在 python 中求解微分方程
- javascript - 来自javascript的json记录,espace为空记录
- .net - 如何避免将包含 EF 配置的 dll.config 文件放入 bin 输出目录?
- java - 为什么我的 HttpSecurity 不应用管理路径的配置?