Skip to content

Instantly share code, notes, and snippets.

@KunihikoKido
Created August 2, 2013 02:55
Show Gist options
  • Save KunihikoKido/6137195 to your computer and use it in GitHub Desktop.
Save KunihikoKido/6137195 to your computer and use it in GitHub Desktop.
Python 辞書型のソート
def dictsorted(dic, valuesort=False):
if valuesort:
return [(k, v) for k, v in sorted(d.items(), key=lambda x:x[1])]
return [(k, v) for k, v in sorted(d.items())]
if __name__ == '__main__':
d = {'A':500, 'C':300, 'D':100, 'E':400, 'B':200}
print dictsorted(d, False)
print dictsorted(d, True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment