Home  >  Article  >  Backend Development  >  How to implement dictionary sorting in python code description

How to implement dictionary sorting in python code description

伊谢尔伦
伊谢尔伦Original
2017-06-28 13:44:372048browse

This article mainly introduces the implementation method of python dictionary sorting, and analyzes the related skills of Python dictionary sorting with examples. Friends in need can refer to

This article analyzes the Python dictionary sorting with examples. Methods. Share it with everyone for your reference. The details are as follows:

1. Preparatory knowledge:

In python, dictionary is a built-in data type, which is an unordered storage structure , each element is a key-value pair:
For example: dict = {'username': 'password', 'database': 'master'}, where 'username' and 'database' are keys, and 'password' and 'master' is value. You can get the reference of the corresponding value through d[key], but you cannot get the key through value.

For dictionnary, you need to know the following pointsNotes:

a. The key of dictionary is case-sensitive;
b. There cannot be in a dictionary Duplicate keys;
c. Dictionaries are unordered and have no concept of element order. They are just simple arrangements of order pairs.

2. Dictionary sorting implementation:

As explained before, the dictionary itself has no concept of order, but it always happens at some point, but we often need to sort the dictionary. How to do it? Let me tell you below:

Method 1: The simplest method, arrange the elements (key/value pairs), and then pick out the value. The dictionary's items method returns a list of tuples, each tuple containing a pair of items - a key and a corresponding value. At this time, the sort() method can be used for sorting.

def sortedDictValues1(adict):
  items = adict.items()
  items.sort()
  return [value for key, value in items]

Method 2: Use the key arrangement method to select the value, which is faster than method 1. The keys() method of dictionaryobject returns a list of all key values ​​in the dictionary, in random order. When you need to sort, just use the sort() method on the returned key value list.

def sortedDictValues1(adict):
  keys = adict.keys()
  keys.sort()
  return [adict[key] for key in keys]

Method 3: Use the mapping method to perform the last step more efficiently

def sortedDictValues1(adict):
  keys = adict.keys()
  keys.sort()
  return map(adict.get,keys)

Method 4: Sort the dictionary by key, return it in the form of a tuple list, and use the lambda function to perform ;
sorted(iterable[, cmp[, key[, reverse]]]
cmp and key generally use lambda
For example:

>>> d={"ok":1,"no":2}#对字典按键排序,用元组列表的形式返回
>>> sorted(d.items, key=lambda d:d[0])
[('no', 2), ('ok', 1)]

To sort the dictionary by value, use a tuple list The form returns

>>> sorted(d.items, key=lambda d:d[1])
[('ok', 1), ('no', 2)]

Although there are many ways to sort dictionary elements, they are not summarized here. However, if you do not have high requirements for program efficiency, just choose the one you like

.

The above is the detailed content of How to implement dictionary sorting in python code description. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn