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]
  • 相关阅读:
    context:component-scan报错
    goland 实用键
    React-Native 指定模拟器RUN-IOS
    mac 卸载编辑器卸不干净
    go 区分指针
    go 学习Printf
    我的命令行
    mysql8的坑
    小三角
    eslint 禁用命令
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14157761.html
Copyright © 2011-2022 走看看