zoukankan      html  css  js  c++  java
  • zip函数和sorted函数

    ###zip函数
    如果处理两个列表的话就以列表的形式输出
    比如
    list_a = [1,2,3,4,5]
    list_b = ['a','b','c','d','e']
    list_c = zip(list_b,list_a)
    输出结果为:[('a', 1), ('b', 2), ('c', 3), ('d', 4),('e',5)]
    # 合并两个字符串,以字典类型输出
    str_a = "123456"
    str_b = "abcdef"
    str_c = zip(str_a,str_b)
    输出结果为:{1:'a',2:'b',3:'c',4:'d',5:'e',6:'f'}
    # 使用zip()和sorted()对字典排序
    dict_a = {'a': '4', 'b': '1', 'c': '3', 'd': '2'}
    print("直接取字典最小值:", min(dict_a.items()))
    print("直接对字典排序:", sorted(dict_a.items()))
     
    list_temp = zip(dict_a.values(), dict_a.keys())
    print("zip处理后的最小值:", min(list_temp))
     
    list_temp = zip(dict_a.values(), dict_a.keys())
    list_temp = sorted(list_temp)
    print("zip处理后的排序:", list_temp)
    print("zip处理后的最小两个:", list_temp[0:2])
    
    结果为:
    直接取字典最小值: ('a', '4')
    直接对字典排序: [('a', '4'), ('b', '1'), ('c', '3'), ('d', '2')]
    zip处理后的最小值: ('1', 'b')
    zip处理后的排序: [('1', 'b'), ('2', 'd'), ('3', 'c'), ('4', 'a')]
    zip处理后的最小两个: [('1', 'b'), ('2', 'd')]
    以上内容作为课堂笔记,如有雷同,请联系于我
  • 相关阅读:
    Ubuntu系统下的实用软件推荐
    上传项目到GitHub
    eclipse+fileSyncPlugin+svn+jenkins+tomcat
    js,jq获取元素位置属性及兼容性写法
    清除浮动的几种方法
    sub,dl,dt,排版,横向滚动条,浮动元素居中,box-sizing
    a里面不能嵌套a
    随笔
    自适应屏幕轮播图详解
    tip
  • 原文地址:https://www.cnblogs.com/ArtisticMonk/p/8955490.html
Copyright © 2011-2022 走看看