Home>Article>Backend Development> About Python sorting method to find the largest value, the second largest value
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!