Home  >  Article  >  Backend Development  >  About Python sorting method to find the largest value, the second largest value

About Python sorting method to find the largest value, the second largest value

高洛峰
高洛峰Original
2018-05-18 14:57:213144browse

This article mainly introduces in detail the method of Python sorting to find the maximum and second largest value. Friends who need it can refer to it

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)

Results:

[6, 7, 9, 4, 2, 1, 11]

[1, 2, 4, 6, 7, 9, 11]

[11, 9, 7, 6, 4, 2, 1]

The above is the detailed content of About Python sorting method to find the largest value, the second largest value. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn