Python程序按字母顺序对元组列表进行排序
给定一个元组列表,编写一个Python程序,按每个元组的第一项按字母顺序对元组进行排序。
例子:
Input: [("Amana", 28), ("Zenat", 30), ("Abhishek", 29), ("Nikhil", 21), ("B", "C")]
Output: [('Amana', 28), ('Abhishek', 29), ('B', 'C'), ('Nikhil', 21), ('Zenat', 30)]
Input: [("aaaa", 28), ("aa", 30), ("bab", 29), ("bb", 21), ("csa", "C")]
Output: [('aa', 30), ('aaaa', 28), ('bab', 29), ('bb', 21), ('csa', 'C')]
方法一:使用冒泡排序
使用冒泡排序技术我们可以执行排序。请注意,每个元组都是给定列表中的一个元素。使用嵌套循环访问每个元组的第一个元素。这将执行就地排序方法。时间复杂度类似于冒泡排序,即O(n^2)
。
# Python program to sort a
# list of tuples alphabetically
# Function to sort the list of
# tuples
def SortTuple(tup):
# Getting the length of list
# of tuples
n = len(tup)
for i in range(n):
for j in range(n-i-1):
if tup[j][0] > tup[j + 1][0]:
tup[j], tup[j + 1] = tup[j + 1], tup[j]
return tup
# Driver's code
tup = [("Amana", 28), ("Zenat", 30), ("Abhishek", 29),
("Nikhil", 21), ("B", "C")]
print(SortTuple(tup))
输出:
[('Abhishek', 29), ('Amana', 28), ('B', 'C'), ('Nikhil', 21), ('Zenat', 30)]
方法二:使用 sort() 方法
通过这种方法进行排序时,元组的实际内容会发生变化,并且就像之前的方法一样,执行排序的就地方法。
# Python program to sort a list of
# tuples using sort()
# Function to sort the list
def SortTuple(tup):
# reverse = None (Sorts in Ascending order)
# key is set to sort using first element of
# sublist lambda has been used
tup.sort(key = lambda x: x[0])
return tup
# Driver's code
tup = [("Amana", 28), ("Zenat", 30), ("Abhishek", 29),
("Nikhil", 21), ("B", "C")]
print(SortTuple(tup))
输出:
[('Abhishek', 29), ('Amana', 28), ('B', 'C'), ('Nikhil', 21), ('Zenat', 30)]
方法 3:使用 sorted() 方法
sorted()
方法是列表类的一种方法,它返回排序后的列表而不对原始列表进行任何更改。
# Python program to sort a list of
# tuples using sorted()
# Function to sort the list
def Sort_Tuple(tup):
# reverse = None (Sorts in Ascending order)
# key is set to sort using first element of
# sublist lambda has been used
return(sorted(tup, key = lambda x: x[0]))
# Driver Code
tup = [("Amana", 28), ("Zenat", 30), ("Abhishek", 29),
("Nikhil", 21), ("B", "C")]
# printing the sorted list of tuples
print(Sort_Tuple(tup))
输出:
[('Abhishek', 29), ('Amana', 28), ('B', 'C'), ('Nikhil', 21), ('Zenat', 30)]