提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 一、字典排序
  • 二、题目
  • 总结
  • —参考文档:https://blog.csdn.net/weixin_38739735/article/details/117005077

    一、字典排序

    Dictionary 是一种重要的数据结构,它通过将 key 与 value 进行映射来存储数据。Python 中的默认字典是无序数据结构。
    我们有两种对列表进行排序的方法,一种是使用 sort()进行 in-place 排序,另一种是使用 sorted() ,这不是 in-place 排序。不同之处在于,当使用 sort()时,您将更改原始列表,而 sorted()将返回一个新列表,而不更改原始列表。

    二、题目

    第一行一个4

    renshu=int(input())
    #此处不能用map(int,input())
    
    

    map(int,xxx)将元组转list,字符串转list,提取字典的key,做list
    https://blog.csdn.net/quanlingtu1272/article/details/95482253
    第二行四个身高

    sg=[int(i) for i in input().split()]
    

    第三行四个名字

    mz=[i for i in input().split()]
    

    要求按身高从小到大排序,身高相同则按名字abc来排

    
    ## 2.读入数据
    代码如下(示例):
    ```python
    for t in range(renshu):
        dic[mz[t]]=sg[t]
    a1=sorted(dic.items(),key=lambda x: (x[1],(x[0],reversed) ))
    px=[]
    for i in range(renshu):
        px.append(a1[i][0])
    print(px)
    

    重点在a1的写法
    https://blog.csdn.net/LHJCSDNYL/article/details/122525942


    结果示例

    总结

    字符转换参考文档:

    zip的用法,返回元组:
    https://www.runoob.com/python/python-func-zip.html
    可用dict{}将zip字典化

    python-Numpy学习之(一)ndim、shape、dtype、astype的用法

    来源:从此刻积累

    物联沃分享整理
    物联沃-IOTWORD物联网 » python字典排序方法

    发表评论