reduce、map、zip、filter使用记录
注意:结果取完一次就没了:
# -*- coding:utf-8 -*-
### functools.reduce from functools import reduce r1 = reduce(lambda x,y:x+y,[1,2,3,4,5]) print(r1) # 15 r2 = reduce(lambda x,y:x*y,[1,2,3,4,5]) print(r2) # 120 ### map r3 = map(lambda x:x**2,[1,2,3]) print(r3,list(r3)) # <map object at 0x00000155BC0EBC50> [1, 4, 9] print(list(r3)) # [] —— 取完一次就没得了! ### zip r4 = zip((1,2,3),("wanghw","naruto","sasuke")) print(r4,dict(r4)) # <zip object at 0x00000155BC0FAC88> {1: 'wanghw', 2: 'naruto', 3: 'sasuke'} print(dict(r4)) # {} —— 取完一次就有没得了! r5 = zip((4,5,6),("whw","naroo","sausu")) print(r4,list(r5)) # <zip object at 0x00000155BC0FAC88> [(4, 'whw'), (5, 'naroo'), (6, 'sausu')] print(list(r5)) # [] —— 取完一次就没得了! ### filter r6 = filter(lambda x:x>2,[1,2,3,4,5]) print(r6,list(r6)) # <filter object at 0x0000022E538E1B38> [3, 4, 5] print(list(r6)) # [] —— 取完一次就没得了!