我有整數串列,我首先想從中獲得唯一的數字,首先按它們的出現排序,然后計數相等的數字應按降序排列。
示例 1:
input1 = [1,2,2,1,6,2,1,7]
預期輸出 = [2,1,7,6]
解釋:2 和 1 出現三次,而 6 和 7 出現一次。因此,出現三次的數字將按降序排列;對于出現一次的集合也是如此。
另一個例子:
input_2 = list(map(int, '40 29 2 44 30 79 46 85 118 66 113 52 55 63 48 99 123 51 110 66 40 115 107 46 6 114 36 99 13 108 85 39 14 121 42 37 56 11 104 28 24 123 63 51 118 52 120 28 64 43 44 86 42 71 101 78 93 1 6 14 42 33 88 107 35 70 74 30 54 76 27 91 115 71 63 103 94 109 39 4 16 108 97 83 29 57 86 121 53 94 28 7 5 31 123 21 2 17 112 104 75 124 88 30 108 14 65 118 28 81 80 14 14 107 21 60 47 97 50 53 19 112 43 46'.split()))
output_2 = list(map(int, '14 28 123 118 108 107 63 46 42 30 121 115 112 104 99 97 94 88 86 85 71 66 53 52 51 44 43 40 39 29 21 6 2 124 120 114 113 110 109 103 101 93 91 83 81 80 79 78 76 75 74 70 65 64 60 57 56 55 54 50 48 47 37 36 35 33 31 27 24 19 17 16 13 11 7 5 4 1'.split()))
這是來自我參加的編碼測驗。這必須在不使用諸如等匯入的函式的情況下collections解決itertools。并且允許使用 python 命名空間中已經可用的函式,例如dict, 。sorted我如何盡可能有效地做到這一點?
uj5u.com熱心網友回復:
def sort_sort(input1):
a = {i:input1.count(i) for i in set(input1)}
b ={i:[] for i in set(a.values())}
for k,v in a.items():
b[v].append(k)
for v in b.values():
v.sort(reverse=True)
output=[]
quays =list(b.keys())
quays.sort(reverse=True)
for q in quays:
output =b[q]
print(output)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/469368.html
標籤:python-3.x 表现
下一篇:我正在創建一個通過用戶輸入呼叫函式的文本冒險游戲。在start()中輸入1會呼叫start_observe,但2會呼叫start()。為什么?
