列印列表中出現前十的元素,可以利用列表的count屬性,可以計數,然後根據數量多少排序 來輸出元素
a = [1,"b","b",3,3,3,"d","d","d","d",5,5,5,5,5,"f","f","f","
f","f","f",7,7,7,7,7,7,7,"h","h","h","h","h","h","h","h",9,9
,9,9,9,9,9,9,9,"j","j","j","j","j","j","j","j","j","j",11,11
,11,11,11,11,11,11,11,11,11]
b = {} #定義空字典
for d in set(a): #去重複的值,set
b[a.count(d)] = d #去重後做計數,把數量和值寫到字典b
for e in reversed(sorted(b.keys())[-10:]):
print e,":",b[e] #排序列表鍵值並取後10個(數量最大的10個),翻轉後打印出數量與值。
列印列表中出現前十的元素,可以利用列表的count屬性,可以計數,然後根據數量多少排序 來輸出元素
a = [1,"b","b",3,3,3,"d","d","d","d",5,5,5,5,5,"f","f","f","
f","f","f",7,7,7,7,7,7,7,"h","h","h","h","h","h","h","h",9,9
,9,9,9,9,9,9,9,"j","j","j","j","j","j","j","j","j","j",11,11
,11,11,11,11,11,11,11,11,11]
b = {} #定義空字典
for d in set(a): #去重複的值,set
b[a.count(d)] = d #去重後做計數,把數量和值寫到字典b
for e in reversed(sorted(b.keys())[-10:]):
print e,":",b[e] #排序列表鍵值並取後10個(數量最大的10個),翻轉後打印出數量與值。