兩個列表 A 和 B 并且都按升序排列。創建一個新列表,其中包含 A 和 B 的所有元素并且已排序。Example 2 1361
3 回答

月關寶盒
TA貢獻1772條經驗 獲得超5個贊
這是該問題的潛在解決方案。las,您還應該發布自己的代碼嘗試。
n1 = int(input())
n2 = int(input())
l1 = [int(input()) for i in range(n1)]
l2 = [int(input()) for i in range(n2)]
print(sorted(l1+l2))

撒科打諢
TA貢獻1934條經驗 獲得超2個贊
嘗試使用heapq:
n1 = int(input())
n2 = int(input())
list1 = [int(input()) for _ in range(n1)]
list2 = [int(input()) for _ in range(n2)]
from heapq import merge
res = list(merge(list1, list2))
或者
list1.extend(list2)
sorted(list1)

開心每一天1111
TA貢獻1836條經驗 獲得超13個贊
要合并兩個列表,您需要此代碼:
list = list2 + list3 print(list)
要排序使用這個:
list.sort() # replace 'list' with your list name
要按降序排序:
list.sort(reverse=True) # replace 'list' with your list name
添加回答
舉報
0/150
提交
取消