FROM:https://www.pythoncentral.io/how-to-sort-python-dictionaries-by-key-or-value/

AND https://www.pythoncentral.io/how-to-sort-a-list-tuple-or-object-with-sorted-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.

numbers = {'first': 1, 'second': 2, 'third': 3, 'Fourth': 4}

>>> sorted(numbers)

['Fourth', 'first', 'second', 'third']
 
>>> sorted(numbers.values())

[1, 2, 3, 4]
 
>>> sorted(numbers, key=numbers.__getitem__)

# In order of sorted values: [1, 2, 3, 4]
['first', 'second', 'third', 'Fourth']
 
# Uses the first element of each tuple to compare
>>> [value for (key, value) in sorted(numbers.items())]
[4, 1, 2, 3]
# In order of sorted keys: ['Fourth', 'first', 'second', 'third']
 
>>> sorted(numbers, key=numbers.__getitem__, reverse=True)
['Fourth', 'third', 'second', 'first']
>>> [value for (key, value) in sorted(numbers.items(), reverse=True)]
[3, 2, 1, 4]
reverse 标识若为true,顺序为反向排序
 
# Won't change the items to be returned, only while sorting
>>> sorted(numbers, key=str.lower)
['first', 'Fourth', 'second', 'third']
 
>>> month = dict(one='January',
                 two='February',
                 three='March',
                 four='April',
                 five='May')
>>> numbermap = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
>>> sorted(month, key=numbermap.__getitem__)
['one', 'two', 'three', 'four', 'five']
同时我们可以对一些字典进行利用一些赋值的数据进行权值排序
 
# Assuming the keys in both dictionaries are EXACTLY the same:
>>> [month[i] for i in sorted(month, key=numbermap.__getitem__)]
['January', 'February', 'March', 'April', 'May']
 
 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 sorted key argument:
 
def repeats(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) for letter in uniques]
 
    return max(counts)
 
# From greatest to least repeats
>>> sorted(month.values(), key=repeats, reverse=True)
['February', 'January', 'March', 'April', 'May']

More advanced sorting functionality

def evens1st(num):
    # Test with modulus (%) two
    if num == 0:
        return -2
    # It's an even number, return the value
    elif num % 2 == 0:
        return num
    # It's odd, return the negated inverse
    else:
        return -1 * (num ** -1)
 
# Max class size first
>>> sorted(trans.values(), key=evens1st, reverse=True)
[30, 24, 33, 7, 0]
Sorting a List(or Tuple) of Custom Python Objects
class Custom(object):
    def __init__(self, name, number):
        self.name = name
        self.number = number
 
    def __repr__(self):
        return '{}: {} {}'.format(self.__class__.__name__,
                                  self.name,
                                  self.number)
def getKey(custom):
    return custom.number
 
>>> sorted(customlist, key=getKey)
[Custom: michael 1, Custom: life 42,
Custom: theodore the great 59, Custom: object 99]
 
Or maybe you feel it's nit-picking,and don't want to type the key keyword everytime,
Redifine our project one more time like this
 
class Custom(object):
    def __init__(self, name, number):
        self.name = name
        self.number = number
 
    def __repr__(self):
        return '{}: {} {}'.format(self.__class__.__name__,
                                  self.name,
                                  self.number)
 
    def __cmp__(self, other):
        if hasattr(other, 'number'):
            return self.number.__cmp__(other.number)
 
>>> sorted(customlist)
[Custom: michael 1, Custom: life 42, Custom: theodore the great 59, Custom: object 99]

Sorting a Heterogeneous List of Custom Python Objects

class AnotherObject(object):
    def __init__(self, tag, age, rate):
        self.tag = tag
        self.age = age
        self.rate = rate
    def __repr__(self):
        return '{}: {} {} {}'.format(self.__class__.__name__,
                                     self.tag,
                                     self.age, self.rate)
    def __cmp__(self, other):
        if hasattr(other, 'age'):
            return self.age.__cmp__(other.age)
customlist = [
    Custom('object', 99),
    Custom('michael', 1),
    Custom('theodore the great', 59),
    Custom('life', 42),
    AnotherObject('bananas', 37, 2.2),
    AnotherObject('pants', 73, 5.6),
    AnotherObject('lemur', 44, 9.2)
]
try it,and ...error:
>>> sorted(customlist)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required
 
Why? Because Custom doesn't have an attribute called age and AnotherObject doesn't have an attribute called number.

Let's redefine those objects again!

class Custom(object):
    def __init__(self,name,number):
        self.name = name
        self.number = number
 
    def __repr__(self):
        return '{}: {} {}'.format(self.__class__.__name__,
                                  self.name,
                                  self.number)
 
    def __cmp__(self, other):
        if hasattr(other, 'getKey'):
            return self.getKey().__cmp__(other.getKey())
 
    def getKey(self):
        return self.number
 
 
class AnotherObject(object):
    def __init__(self, tag, age, rate):
        self.tag = tag
        self.age = age
        self.rate = rate
 
    def __repr__(self):
        return '{}: {} {} {}'.format(self.__class__.__name__,
                                     self.tag,
                                     self.age, self.rate)
 
    def __cmp__(self, other):
        if hasattr(other, 'getKey'):
            return self.getKey().__cmp__(other.getKey())
 
    def getKey(self):
        return self.age

>>> sorted(customlist)
[Custom: michael 1, AnotherObject: bananas 37 2.2,
Custom: life 42, AnotherObject: lemur 44 9.2,
Custom: theodore the great 59, AnotherObject: pants 73 5.6,
Custom: object 99]
And it finally Success
 
You can do that too. If you leave out the __cmp__ functions in each object, and define an outside function like so:
def getKey(customobj):
    return customobj.getKey()\
And then call sorted like so:
>>> sorted(customlist, key=getKey)
[Custom: michael 1, AnotherObject: bananas 37 2.2,
Custom: life 42, AnotherObject: lemur 44 9.2,
Custom: theodore the great 59, AnotherObject: pants 73 5.6,
Custom: object 99]

最新文章

  1. MongoDB【第二篇】MongoDB逻辑与物理存储结构
  2. spring使用cache
  3. IIS上部署MVC网站,打开后ExtensionlessUrlHandler-Integrated-4.0解决方法IIS上部署MVC网站,打开后500错误
  4. 自编译ngrok服务器
  5. hdu 5327 Olympiad
  6. Using an Interface as a Type
  7. Eclipse jetty
  8. vim中设置Python自动补全
  9. Linux内核--网络栈实现分析(二)--数据包的传递过程--转
  10. PowerDesigner 如何添加每个表中共用的字段及自动添加注释
  11. php递归数组中的应用
  12. Java基础知识强化之IO流笔记12:递归之递归解决问题的思想(图解)
  13. 实战:mysql版本号升级
  14. ajax请求返回数据,模板中的数据处理
  15. Python 面向对象(四) 反射及其魔术方法
  16. Navicat连接到服务器端数据库
  17. sort 快排解决百万级的排序
  18. Atitit 数据融合merge功能v3新特性.docx
  19. Android WebView无法播放视频或直播,关闭界面后任在播放的问题;
  20. python 文件操作: 文件操作的函数, 模式及常用操作.

热门文章

  1. TypeScript 迭代器(iterator)和生成器(generator)
  2. LC 3. Longest Substring Without Repeating Characters
  3. Django之ORM相关操作
  4. idea jetty:run 启动
  5. Shell重定向:2&gt;&amp;1
  6. 移动端 H5 上拉刷新,下拉加载
  7. 减少打包组件vue.config.js——Webpack的externals的使用
  8. Java 18套JAVA企业级大型项目实战分布式架构高并发高可用微服务电商项目实战架构
  9. Django路由及函数视图
  10. vim文件时自动添加作者、时间、版权等信息