这篇文章主要为大家详细介绍了关于python排序求最大值第二最大的方法,需要的朋友可以参考下
nums = [6, 11, 7 ,9, 4, 2,1]
i = len(nums) - 1
j = 1
while j < i:
if nums[j] > nums[j+1]:
nums[j], nums[j+1] = nums[j+1], nums[j]
j += 1
print(nums)
lst = sorted(nums)
print(lst)
lst = sorted(nums, reverse=True)
print(lst)结果:
[6, 7, 9, 4, 2, 1, 11]
[1, 2, 4, 6, 7, 9, 11]
[11, 9, 7, 6, 4, 2, 1]
以上就是关于python排序求最大值第二最大的方法的详细内容,更多请关注php中文网其它相关文章!
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号