Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to proceed
#4
import string
import operator
import collections


# example data
chars = {c: n for n, c in enumerate(string.ascii_uppercase)}

sorted_chars = sorted(chars.items(), key=operator.itemgetter(1))
# chars.items() returns a list with tuples
# each tuple is a key-value pair
# the sorted function accepts a key function for sorting
# operator.itemgetter(1) returns a function, which returns
# the second element of a list

# if you want to have a "sorted" dict, you should use OrderedDict from collections
sorted_chars_dict = collections.OrderedDict(sorted_chars)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
How to proceed - by jarrod0987 - Jan-16-2018, 10:55 PM
RE: How to proceed - by Larz60+ - Jan-17-2018, 01:49 AM
RE: How to proceed - by jarrod0987 - Jan-17-2018, 01:33 PM
RE: How to proceed - by DeaD_EyE - Jan-17-2018, 02:05 PM
RE: How to proceed - by jarrod0987 - Jan-17-2018, 11:04 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020