본문 바로가기

IT/프로그래밍

[python] dict 를 sort해서 list로 넣기

728x90

원글(이글루): 2010-11-30 15:03:27

def sortByKey(dict, bReverse):
  value_key_list = []
  for key in dict.keys():
    value_key_list.append (( key,dict[key] ))
  value_key_list.sort()
  if ( bReverse ):
    value_key_list.reverse()
  return value_key_list

def sortByValue(dict, bReverse):
  value_key_list = []
  for key in dict.keys():
    value_key_list.append( ( dict[key] , key) )
  value_key_list.sort()
  if ( bReverse ):
    value_key_list.reverse()
  return value_key_list

def printList(list, bOrder):
  if (bOrder):
    for i in list:
      print i[1], i[0]
  else:
    for i in list:
      print i[0], i[1]

728x90