喜欢 C++ SO() , Java排序() 和其他语言一样,python还提供了用于排序的内置函数。
null
排序功能可用于按升序和降序对列表进行排序。
按升序对列表进行排序。
语法
#这将按升序对给定列表进行排序。 #它根据传递的参数返回一个排序列表。 列出你的名字。排序()
此函数可用于对整数、浮点数、字符串等的列表进行排序。
# List of Integers numbers = [ 1 , 3 , 4 , 2 ] # Sorting list of Integers numbers.sort() print (numbers) # List of Floating point numbers decimalnumber = [ 2.01 , 2.00 , 3.67 , 3.28 , 1.68 ] # Sorting list of Floating point numbers decimalnumber.sort() print (decimalnumber) # List of strings words = [ "Geeks" , "For" , "Geeks" ] # Sorting list of strings words.sort() print (words) |
输出:
[1, 2, 3, 4] [1.68, 2.0, 2.01, 3.28, 3.67] ['For', 'Geeks', 'Geeks']
按降序排列列表。
语法
list_name.sort(reverse=True) This will sort the given list in descending order.
# List of Integers numbers = [ 1 , 3 , 4 , 2 ] # Sorting list of Integers numbers.sort(reverse = True ) print (numbers) # List of Floating point numbers decimalnumber = [ 2.01 , 2.00 , 3.67 , 3.28 , 1.68 ] # Sorting list of Floating point numbers decimalnumber.sort(reverse = True ) print (decimalnumber) # List of strings words = [ "Geeks" , "For" , "Geeks" ] # Sorting list of strings words.sort(reverse = True ) print (words) |
输出:
[4, 3, 2, 1] [3.67, 3.28, 2.01, 2.0, 1.68] ['Geeks', 'Geeks', 'For']
语法:
列出你的名字。sort()–按升序排序 列出你的名字。排序(reverse=True)–按降序排序 列出你的名字。排序(key=…,reverse=…)——它根据用户的选择进行排序
参数: 默认情况下,sort()不需要任何额外的参数。但是,它有两个可选参数:
颠倒 –如果为true,则列表按降序排序 钥匙 –用作排序比较键的函数
# Python program to demonstrate sorting by user's # choice # function to return the second element of the # two elements passed as the parameter def sortSecond(val): return val[ 1 ] # list1 to demonstrate the use of sorting # using using second key list1 = [( 1 , 2 ),( 3 , 3 ),( 1 , 1 )] # sorts the array in ascending according to # second element list1.sort(key = sortSecond) print (list1) # sorts the array in descending according to # second element list1.sort(key = sortSecond,reverse = True ) print (list1) |
输出:
[(1, 1), (1, 2), (3, 3)] [(3, 3), (1, 2), (1, 1)]
请参考 Python排序 更多Python排序文章。
幸亏 奋斗者 关于这个话题的意见。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END