在Python中创建两个未排序列表的排序合并列表
我们需要在Python中获取两个列表并将它们合并为一个。最后,我们显示排序列表。
例子:
Input :
list1 = [25, 18, 9, 41, 26, 31]
list2 = [25, 45, 3, 32, 15, 20]
Output :
[3, 9, 15, 18, 20, 25, 25, 26, 31, 32, 41, 45]
Input :
list1 = ["suraj", "anand", "gaurav", "aman", "kishore"]
list2 = ["rohan", "ram", "mohan", "priya", "komal"]
Output :
['aman', 'anand', 'gaurav', 'kishore', 'komal',
'mohan', 'priya', 'ram', 'rohan', 'suraj']
Python中的加号运算符“+”帮助我们合并两个列表,无论是字符串列表还是整数列表或两者的混合。最后,我们使用sort()函数对列表进行排序。
# Python program to merge and sort two list
def Merge(list1, list2):
final_list = list1 + list2
final_list.sort()
return(final_list)
# Driver Code
list1 = [25, 18, 9, 41, 26, 31]
list2 = [25, 45, 3, 32, 15, 20]
print(Merge(list1, list2))
输出:
[3, 9, 15, 18, 20, 25, 25, 26, 31, 32, 41, 45]