Home > Backend Development > Python Tutorial > 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

高洛峰
Release: 2018-05-18 14:57:21
Original
3186 people have browsed it

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)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template