"""代碼這么寫完全就不用寫死了,隨便什么字典都可以進行排序,只需要更換字典即可"""
#獲取原有字典d值
d?=?{
????'Adam':?95,
????'Lisa':?85,
????'Bart':?59
}
#封裝bubble冒泡排序算法,此方法輸入list返回list,返回list的值從大到小排列,如果需要從小到大排列修改方法中的if即可
def?bubble(bubbleList):
????listLength?=?len(bubbleList)
????while?listLength?>?0:
????????for?i?in?range(listLength?-?1):
????????????if?bubbleList[i]?<?bubbleList[i+1]:
????????????????bubbleList[i]?=?bubbleList[i]?+?bubbleList[i+1]
????????????????bubbleList[i+1]?=?bubbleList[i]?-?bubbleList[i+1]
????????????????bubbleList[i]?=?bubbleList[i]?-?bubbleList[i+1]
????????listLength?-=?1
????return?bubbleList
#將原有的字典轉換成1個list,排序以value進行,那么將d字典中的所有value進行list轉換
list_value?=?list(d.values())
#將轉換后的list_value傳入到bubble冒泡排序算法中進行排序
bubble_list?=?bubble(list_value)
#?print?bubble_list?#這里去掉之前的注釋能看到排序后的效果
#將排序后的bubble_list列表使用for循環進行字典值的動態匹配,并將匹配的值進行輸出打印
for?value1?in?bubble_list:
????for?(key,value2)?in?d.iteritems():
????????if?value1?==?value2:
????????????print?"%s,%d"?%?(key,value2)
2017-03-14
老哥穩
2017-03-14
老哥穩