python笔记——enumerate,heapq,itertools,Counter用法

    技术2022-07-15  71

    names=['关羽','张飞','赵云','马超','黄忠'] courses=['语文','数学','英语'] socres=[[None]*len(courses)for _ in range(len(names))] print(socres) for row,name in enumerate(names): for col,course in enumerate(courses): socres[row][col]=float(input(f'请输入{name}的{course}成绩:')) print(socres) import heapq list1=[34,25,12,99,87,63,58,78,88,92] list2 = [ {'name': 'IBM', 'shares': 100, 'price': 91.1}, {'name': 'AAPL', 'shares': 50, 'price': 543.22}, {'name': 'FB', 'shares': 200, 'price': 21.09}, {'name': 'HPQ', 'shares': 35, 'price': 31.75}, {'name': 'YHOO', 'shares': 45, 'price': 16.35}, {'name': 'ACME', 'shares': 75, 'price': 115.65} ] print(heapq.nlargest(3,list1)) print(heapq.nsmallest(3,list1)) print(heapq.nlargest(2,list2,key=lambda x:x['shares'])) print(heapq.nlargest(2,list2,key=lambda x:x['price'])) import itertools itertools.combinations('ABCD',3) from collections import Counter words = [ 'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes', 'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the', 'eyes', "don't", 'look', 'around', 'the', 'eyes', 'look', 'into', 'my', 'eyes', "you're", 'under' ] counter=Counter(words) print(counter) print(counter.most_common(5))
    Processed: 0.011, SQL: 9