• 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

  • 相关阅读:
    上班中午
    有些确实牛擦的有点嚣张!!! (zz)
    Docker学习笔记(二)
    结对项目第二阶段小结
    软件工程结课总结
    案例分析作业
    结对编程总结
    Docker学习笔记(一)
    四则运算
    构建之法1~5章
  • 原文地址:https://www.cnblogs.com/ryansunyu/p/3902886.html
Copyright © 2020-2023  润新知