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')]
    以上内容作为课堂笔记,如有雷同,请联系于我
  • 相关阅读:
    程序怎么才能把自己的删除掉?
    Winsock编程入门1.初始化Winsock
    关于83版射雕英雄传
    一个感人的爱情故事(中英对照)
    NT系统的命令
    Delphi小巧的Windows NT服务程序源码
    更改Windows 登录屏幕保护程序
    画鬼最易
    濮水垂钓
    现代工作观
  • 原文地址:https://www.cnblogs.com/ArtisticMonk/p/8955490.html
Copyright © 2011-2022 走看看