Python dict sort
2016-04-12 10:54
531 查看
Note: If you want to sort a list, tuple or object in Python, checkout this article: How
to Sort a List or Tuple in Python
The
and retrieve them via keys. The values can be objects of any type (dictionaries can even be nested with other dictionaries) and the keys can be any object so long as it's hashable, meaning basically that it is immutable (so strings are
not the only valid keys, but mutable objects like lists can never be used as keys). Unlike Python lists or tuples, the key and value pairs in
are not in any particular order, which means we can have a
Although the key-value pairs are in a certain order in the instantiation statement, by calling the
on it (which will create a list from its keys) we can easily see they aren't stored in that order:
If we want to order or sort the dictionary objects by their keys, the simplest way to do so is by Python's built-in
which will take any iterable and return a list of the values which has been sorted(in ascending order by default). There is no class method for sorting dictionaries as there is for lists, however the
works the same exact way. Here's what it does with our dictionary:
We can see this method has given us a list of the keys in ascending order, and in almost alphabetical order, depending on what we define as "alphabetical." Also notice that we sortedits list of keys by its keys — if we want to sort its
list of values by its keys, or its list of keys by its values, we'd have to change the way we use the
We'll look at these different aspects of
In the same way as we did with the keys, we can use
This is the list of values in the default order, in ascending order. These are very simple examples so let's now examine some slightly more complex situations where we are sorting our
If we simply give the
other arguments (i.e.
we can get it to perform more complex sorts.
The
us to define specific functions to use when sorting the items, as an iterator (in our
examples above the keys and values were both the items to sort and the items used for comparison, but if we want to sort our
our
do that via its
With this statement we told
keys), and to sort them by using numbers' class method for retrieving values — essentially we told it "for every key in numbers, use the corresponding value in numbers for comparison to sort it.".
We can also sort the values in numbers by its keys, but using the
is no dictionary method to return a key by using a certain value, as with the
comprehension to keep it simple:
Now the other argument to consider is the
the order will be reversed (descending), otherwise if it's
that. For example, as with the two previous sorts:
These sorts are still fairly simple, but let's look at some special algorithms we might use with strings or numbers to sort our dictionary.
Sorting strings in alphabetical order is very common, however using
the "proper" alphabetical order, i.e. ignoring the case. To ignore the case we can again utilize the
method so that all strings are the same case when comparing them:
For associating strings with numbers, we need an extra element of context to associate them properly. Let's make a new
first:
This contains only string keys and values, so there'd be no way to put the months in the correct order without additional context. To do so we can simply create another dictionary to map the strings to their numerical values and use that dictionary's
to compare the values in our
As we can see, it still returned a sortedlist of its first argument (month). To return the months in order, we'll use a list
comprehension again:
If we wanted to sort our key/value strings by the number of repeated letters in each string, we could define our own custom method to use in the
Using the function can be performed as follows:
Now let's say we have a dictionary keeping track of the number of students in a class for each of these months, like so:
If we want to organize the class sizes with the even numbers first and odd numbers second, we could do so with a definition like this:
Using the
Likewise we could list the odd class sizes first, and perform many other algorithms to get our sort exactly how we want. There are many other intricate sorting methods and tricks you could use with dictionaries (or any iterable object), but for now hopefully
these examples have provided a good starting point.
to Sort a List or Tuple in Python
The
dict(dictionary) class object in Python is a very versatile and useful container type, able to store a collection of values
and retrieve them via keys. The values can be objects of any type (dictionaries can even be nested with other dictionaries) and the keys can be any object so long as it's hashable, meaning basically that it is immutable (so strings are
not the only valid keys, but mutable objects like lists can never be used as keys). Unlike Python lists or tuples, the key and value pairs in
dictobjects
are not in any particular order, which means we can have a
dictlike this:
1 | numbers={'first':1,'second':2,'third':3,'Fourth':4} |
listmethod
on it (which will create a list from its keys) we can easily see they aren't stored in that order:
1 2 | >>>list(numbers) ['second','Fourth','third','first'] |
Sorting Python dictionaries by Keys
If we want to order or sort the dictionary objects by their keys, the simplest way to do so is by Python's built-in sortedmethod,
which will take any iterable and return a list of the values which has been sorted(in ascending order by default). There is no class method for sorting dictionaries as there is for lists, however the
sortedmethod
works the same exact way. Here's what it does with our dictionary:
1 2 3 | #This is the same as calling sorted(numbers.keys()) >>>sorted(numbers) ['Fourth','first','second','third'] |
list of values by its keys, or its list of keys by its values, we'd have to change the way we use the
sortedmethod.
We'll look at these different aspects of
sortedin a bit.
Sorting Python dictionaries by Values
In the same way as we did with the keys, we can use sortedto sort the Python dictionary by its values:
1 2 3 | # We have to call numbers.values() here >>>sorted(numbers.values()) [1,2,3,4] |
dictobject.
Custom sorting algorithms with Python dictionaries
If we simply give the sortedmethod the dictionary's keys/values as an argument it will perform a simple sort, but by utilizing its
other arguments (i.e.
keyand
reverse)
we can get it to perform more complex sorts.
The
keyargument (not to be confused with the dictionary's keys) for
sortedallows
us to define specific functions to use when sorting the items, as an iterator (in our
dictobject). In both
examples above the keys and values were both the items to sort and the items used for comparison, but if we want to sort our
dictkeys using
our
dictvalues, then we would tell
sortedto
do that via its
keyargument. Such as follows:
1 2 3 4 | # Use the __getitem__ method as the key function >>>sorted(numbers,key=numbers.__getitem__) # In order of sortedvalues: [1, 2, 3, 4] ['first','second','third','Fourth'] |
sortedto sort the numbers
dict(its
keys), and to sort them by using numbers' class method for retrieving values — essentially we told it "for every key in numbers, use the corresponding value in numbers for comparison to sort it.".
We can also sort the values in numbers by its keys, but using the
keyargument would be more complicated (there
is no dictionary method to return a key by using a certain value, as with the
list.indexmethod). Instead we can use a list
comprehension to keep it simple:
1 2 3 4 | # Uses the first element of each tuple to compare >>>[valuefor(key,value)insorted(numbers.items())] [4,1,2,3] # In order of sortedkeys: ['Fourth', 'first', 'second', 'third'] |
reverseargument. If this is
True,
the order will be reversed (descending), otherwise if it's
Falseit will be in the default (ascending) order, it's as simple as
that. For example, as with the two previous sorts:
1 2 3 4 | >>>sorted(numbers,key=numbers.__getitem__,reverse=True) ['Fourth','third','second','first'] >>>[valuefor(key,value)insorted(numbers.items(),reverse=True)] [3,2,1,4] |
Sorting Python dictionaries with String and Number Algorithms
Sorting strings in alphabetical order is very common, however using sortedmight not sort the keys/values of our
dictin
the "proper" alphabetical order, i.e. ignoring the case. To ignore the case we can again utilize the
keyargument and the
str.lower(or
str.upper)
method so that all strings are the same case when comparing them:
1 2 3 | # Won't change the items to be returned, only while sorting >>>sorted(numbers,key=str.lower) ['first','Fourth','second','third'] |
dictobject
first:
1 2 3 4 5 | >>>month=dict(one='January', two='February', three='March', four='April', five='May') |
__getitem__method
to compare the values in our
monthdictionary:
1 2 3 | >>>numbermap={'one':1,'two':2,'three':3,'four':4,'five':5} >>>sorted(month,key=numbermap.__getitem__) ['one','two','three','four','five'] |
comprehension again:
1 2 3 | # Assuming the keys in both dictionaries are EXACTLY the same: >>>[month[i]foriinsorted(month,key=numbermap.__getitem__)] ['January','February','March','April','May'] |
sortedkey argument:
1 2 3 4 5 6 7 8 9 10 11 | defrepeats(string): # Lower the case in the string string=string.lower() # Get a set of the unique letters uniques=set(string) # Count the max occurrences of each unique letter counts=[string.count(letter)forletterinuniques] returnmax(counts) |
1 2 3 | # From greatest to least repeats >>>sorted(month.values(),key=repeats,reverse=True) ['February','January','March','April','May'] |
More advanced sorting functionality
Now let's say we have a dictionary keeping track of the number of students in a class for each of these months, like so:1 | >>>trans=dict(January=33,February=30,March=24,April=0,May=7) |
1 2 3 4 5 6 7 8 9 10 | defevens1st(num): # Test with modulus (%) two ifnum==0: return-2 # It's an even number, return the value elifnum%2==0: returnnum # It's odd, return the negated inverse else: return-1*(num**-1) |
evens1stsorting function, gives us the following output:
1 2 3 | # Max class size first >>>sorted(trans.values(),key=evens1st,reverse=True) [30,24,33,7,0] |
these examples have provided a good starting point.
相关文章推荐
- Python面向对象特性,走向类
- Python3.5爬虫入门示例-获取百度首页及相关的html页面
- 用Python写冒泡排序代码
- Python sort sorted OrderDict
- Python set方法总结
- Python collections deque - double-ended queue ()
- Selenium2 Python 自动化测试实战学习笔记(二)
- Python unittest中常用到的assert
- Python装饰器
- Python基础 变量和数据类型
- PhantomJS 与python的结合
- Python与自然语言处理(二)基于Gensim的Word2Vec
- Python基础:文件读写
- 【python学习】多线程编程
- python简史
- python面对对象编程------3:写集合类的三种方法
- 简单Python3爬虫程序(5)进阶:知乎网的登录与用户相关信息爬取
- 如何扩展openstack python novaclient
- spark机器学习-前言
- python相关网址