zoukankan      html  css  js  c++  java
  • python中给列表元素排序

     1、永久排序

    >>> test1 = ["ccc","ddd","aaa","bbb"]
    >>> test2 = test1[:]
    >>> test1
    ['ccc', 'ddd', 'aaa', 'bbb']
    >>> test2
    ['ccc', 'ddd', 'aaa', 'bbb']
    >>> test1.sort()  ## 正向排序
    >>> test1
    ['aaa', 'bbb', 'ccc', 'ddd']
    >>> test2.sort(reverse=True)  ## 逆向排序
    >>> test2
    ['ddd', 'ccc', 'bbb', 'aaa']
    >>> test3 = [3,5,1,8,2]
    >>> test5 = test3[:]
    >>> test3
    [3, 5, 1, 8, 2]
    >>> test5
    [3, 5, 1, 8, 2]
    >>> test3.sort()
    >>> test3
    [1, 2, 3, 5, 8]
    >>> test5.sort(reverse=True)
    >>> test5
    [8, 5, 3, 2, 1]

    2、临时排序

    >>> test1 = [32,4,65,24,3,9]
    >>> test1
    [32, 4, 65, 24, 3, 9]
    >>> sorted(test1)    ## 临时排序
    [3, 4, 9, 24, 32, 65]
    >>> test1
    [32, 4, 65, 24, 3, 9]
    >>> test2 = sorted(test1)
    >>> test1
    [32, 4, 65, 24, 3, 9]
    >>> test2
    [3, 4, 9, 24, 32, 65]
    >>> sorted(test1,reverse=True)
    [65, 32, 24, 9, 4, 3]
    >>> test3 = sorted(test1,reverse=True)
    >>> test1
    [32, 4, 65, 24, 3, 9]
    >>> test3
    [65, 32, 24, 9, 4, 3]
  • 相关阅读:
    链路追踪
    Zuul网关
    MyBatis批量插入
    自定义组件使用v-model
    正则表达式入门
    博客园主题1【备份】
    关于我
    input输入框内容规范正则总结
    实例003:完全平方数
    python基础day5dict
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14157761.html
Copyright © 2011-2022 走看看