zoukankan      html  css  js  c++  java
  • DSU

    DSU stands for ‘decorate, sort, undecorate’ and refers to a pattern that is often useful for sorting lists according to some attribute of elements.

    For example, if you have a dictionary that maps from mothers to lists of their children, you might want to sort the mothers by their number of children. Here is a function that does that:

                           

    The first loop assigns each mother to mother and each list of children to children. It builds a list of tuples. Where each tuple is the number of children and a mother. Sort compares the first element, number of children, first, and only considers the second element to break ties. The result of tuples sorted in increasing order by number of children.

    The second loop traverses the list of tuples and builds a list of mothers, sorted by parity (which in this context means number of children).

    This pattern is called ‘decorate, sort, undecorate’ because the first loop ‘decorates’ the list of mothers by pairing each mother with her parity, and the last loop ‘undecorate’ the sorted list by removing the parity information.

    from Thinking in Python

  • 相关阅读:
    奶酪(NOIP2017 Day2 T1)
    图的遍历(某谷P3916)
    20154331 EXP9web安全基础实践
    20154331 EXP8 web基础
    20154331EXP7 网络欺诈
    20154331 Exp6 信息搜集与漏洞扫描
    Exp5 MSF基础应用
    Exp4 恶意代码分析
    Exp3 免杀原理与实践
    20154331黄芮EXP2 后门原理与实践
  • 原文地址:https://www.cnblogs.com/ryansunyu/p/3902886.html
Copyright © 2011-2022 走看看