zoukankan      html  css  js  c++  java
  • python随记

    a = {"a":1, "b":5, "c":3}
    b = sorted(a.items(), key=lambda x:x[1], reverse=True) #sorted用法
    print(b)

    print(min(zip(a.values(), a.keys()))) #找到字典中哪个值最大

    print(max(a.values())) #找到字典中哪个值最大
    print(max(a, key=lambda x:a[x]))        #找到字典里最大值对应的key

    from collections import deque
    import heapq
    from collections import Counter
    from operator import itemgetter
    a = {"a":1, "b":5, "c":3}
    b = {"e":2, "b":5, "d":4}
    # b = sorted(a.items(), key=lambda x:x[1], reverse=True) #sorted用法
    # print(b)

    # print(min(zip(a.values(), a.keys()))) #找到字典中哪个值最大

    # print(max(a.values()))
    #
    # print(max(a, key=lambda x:a[x])) #找到字典里最大值对应的key

    # print(a.keys() & b.keys()) #找出二个字典中具有相同的 key
    # print(a.items() & b.items()) #找出二个字典中key,value都相同的项

    # a = [1,2,3,4]
    # b,*e,m = a
    # print(m)

    # c = deque(maxlen=3) #创建一个固定长度的列表
    # c.append(1)
    # c.append(2)
    # c.append(3)
    # c.append(4)
    # print(c)
    #
    # c.appendleft(5) #左边添加一个
    # print(c)
    # c.popleft() #左边删除第一个
    # print(c)

    # nums = [1,2,5,0,8,3,5,2,0,8,9,1]
    # # print(heapq.nlargest(3, nums)) # 找到最大的三个元素
    # # print(heapq.nsmallest(3, nums)) #找到最小的三个元素

    # nums = [(1,5),(2,0),(8,9),(0,6)]
    # print(heapq.nlargest(2, nums, key=lambda x:x[0])) #字典也同样可以操作

    # a = {"a":12, "b":33, "c":11}
    #
    # print(min(zip(a.values(), a.keys()))) #获取字典 value 最小的值
    # b = sorted(zip(a.values(), a.keys())) #字典 value 排序
    #
    # print({key:value for value,key in b})
    #
    # print({value:key for key,value in a.items()}) #字典 key value 翻转


    # words = ['look', 'like', 'he', 'look', 'he', 'look'] #实现单词出现次数统计并排序
    # word_counts = Counter(words)
    # print(word_counts)
    # print(word_counts.most_common(2)) #如果指定为 2 , 就获取出现最多的 2 个单词
    # print(word_counts['look'])


    m = {'look': 3, 'he': 2, 'like': 1}
    m1 = Counter(m)

    m2 = {'look': 1, 'he': 1, 'like': 1}
    m3 = Counter(m2)
    print(m1 + m3) #通过 Counter 可以对字典相同的value进行数据计算

    b = [
    {"a":123, "b":333, "c":53},
    {"a":12, "b":3393, "c":53},
    {"a":12, "b":33343, "c":53},
    ]

    print(sorted(b, key=itemgetter('a'), reverse=True)) #排序列表字典

    print(sorted(b, key=lambda x:(x['a'], x['b'])))




















































  • 相关阅读:
    网络基础
    SQL注入
    OpenID说明
    Linux网络编程
    Linux的僵尸进程产生原因及解决方法
    计算机系统的存储层次
    Java实现SSO
    JD(转载)
    Switch的表达式的要求
    leetcode(23. Merge k Sorted Lists)
  • 原文地址:https://www.cnblogs.com/redhat0019/p/14840201.html
Copyright © 2011-2022 走看看