Python 笔记

 1 #生成式
 2 prices = {
 3     'meat' : 543.2,
 4     'duck' : 123.56,
 5     'dog' : 45.67,
 6     'horse' : 78.55,
 7     'miao' : 123.55
 8 }
 9 price = {key : value for key, value in prices.items() if value > 100}
10 print(price)
11 
12 #嵌套的列表
13 names = ['小明', '小红', '小王']
14 courses = ['英语', '数学', '生物', '物理']
15 chart = [[None]*len(courses) for _ in range(len(names))]
16 for low, name in enumerate(names):
17     for lol, course in enumerate(courses):
18         chart[low][lol] = float(input(f'{name}的{course}成绩为:'))
19 print(chart)
20 
21 #heapq模块(堆排列)
22 import heapq
23 list1 = [44, 23, 56, 34, 33, 55, 234, 45, 24, 46]
24 list2 = [
25 {'name' : '小明', 'shares':45, 'price':234},
26 {'name' : '小红', 'shares':55, 'price':567},
27 {'name' : '小王', 'shares':89, 'price':678},
28 {'name' : '小玉', 'shares':67, 'price':980},
29 ]
30 print(heapq.nlargest(3, list1))
31 print(heapq.nsmallest(3, list1))
32 print(heapq.nlargest(2, list2, key = lambda x :x['shares']))
33 print(heapq.nlargest(2, list2, key=lambda x:x['price']))
34 
35 #itertools模块(迭代工具)
36 import itertools
37 #产生abcd全排列
38 print(itertools.permutations('abcd'))
39 #产生abcde的五选三组合
40 print(itertools.combinations('abcde', 5))
41 #产生abcd和123的笛卡尔积
42 print(itertools.product('abcd', '123'))
43 #产生abc的无限序列
44 print(itertools.cycle(('a','b','c')))

 

posted @ 2021-06-17 23:05  Uriel-w  阅读(18)  评论(0编辑  收藏  举报