美文网首页
数据结构与算法

数据结构与算法

作者: d56eed656c00 | 来源:发表于2017-11-16 17:53 被阅读3次
     p=(1,2)
     x, y =p
     x, y
    (1, 2)
    
    datas = [ 'eric', 'man', (120,112,121)]
    name, sex, (math, english, chinese) = datas
    name, sex, math, english, chinese
    ('eric', 'man', 120, 112, 121)
    
    # *语法
    detial = ['eric', 'man', '10010', '10086']
    name, sex, *phone_number = detial
    print(type(phone_number))#<class 'list'>
    print(phone_number)#['10010', '10086']
    
    from collections import deque
    li = deque()
    li.append(1)
    li.append(2)
    
    li.appendleft(0)
    print(li)#deque([0, 1])
    print(li.pop())#1
    print(li)#deque([0])
    print(li.popleft())#0
    
    

    lambda

    from functools import reduce
    f = lambda x:x+1
    print(f(1))#2
    li = [1,2,3,4]
    
    li_filter = filter(lambda x:x%2 == 0, li)
    print(list(li_filter))#[2, 4]
    
    li_map = map(lambda x:x+1, li)
    print(list(li_map))#[2, 3, 4, 5]
    
    li_reduce = reduce(lambda x, y: x + y, li)
    print(li_reduce)#10 ,python3中线from functools import reduce
    

    heapq

    import heapq
    li = [5,3,6,88,9,12]
    print(heapq.nlargest(2,li))#[88, 12]
    print(heapq.nsmallest(2,li))#[3, 5]
    
    lid = [{'a':15, 'b':23, 'c':33},
          {'a':13, 'b':23, 'c':43},
          {'a':3, 'b':22, 'c':34},
          {'a':21, 'b':42, 'c':39}]
    
    #对列表中的元素以关键字进行排序
    print(heapq.nsmallest(2, lid, key = lambda x: x['a']))
    print(heapq.nlargest(2, lid, key = lambda x: x['a']))
    #[{'c': 34, 'a': 3, 'b': 22}, {'c': 43, 'a': 13, 'b': 23}]
    #[{'c': 39, 'a': 21, 'b': 42}, {'c': 33, 'a': 15, 'b': 23}]
    

    相关文章

      网友评论

          本文标题:数据结构与算法

          本文链接:https://www.haomeiwen.com/subject/qbhrvxtx.html