python常用函式進階(2)之map,filter,reduce,zip

JeffreyLee發表於2019-07-29

Basic Python : Map, Filter, Reduce, Zip

1-Map()

1.1 Syntax
#      fun : a function applying to the iterable object
# iterable : such as list, tuple, string and other iterable object

map(fun, *iterable)     # * token means that multi iterables is supported
1.2 Working

map() applying the given function to each item of the given iterable object.

map() returns an iterable object called "map object".

1.3 Examples
# eg 1
def addition(n): 
    return n + n 

numbers = (1, 2, 3, 4) 
result = map(addition, numbers) 
print(list(result)) 
>>>[2,4,6,8]

# eg 2
numbers = (1, 2, 3, 4) 
result = map(lambda x: x + x, numbers) 
print(list(result)) 
>>>[2,4,6,8]

# eg 3 - multi iterables
numbers1 = [1, 2, 3] 
numbers2 = [4, 5, 6] 

result = map(lambda x, y: x + y, numbers1, numbers2) 
print(list(result))
>>>[5,7,9]

2-Filter()

2.1 Syntax
#      fun : a function that tests if each element of the sequence true or not.
# sequence : who needs to be filtered, it can be sets, lists, tuples, or containers of any iterators.

filter(fun, sequence)
2.2 Working

filter() applying the given function to each item of the given sequence object, and remain the eligible element.

filter() returns an iterator that is already filtered.

2.3 Examples
# eg 1
seq = [0, 1, 2, 3, 5, 8, 13] 

result = filter(lambda x: x % 2 == 0, seq) 
print(list(result)) 
>>>[0, 2, 8]

3-Reduce()

3.1 Syntax
#      fun : a function applying to all elements of the sequence.
# sequence : who needs to be computered by itself, it can be sets, lists, tuples, or containers of any iterators.

filter(fun, sequence)
3.2 Working
  1. At first step, first two elements of sequence are picked and the result is obtained.
  2. Next step is to apply the same function to the previously attained result and the number just succeeding the second element and the result is again stored.
  3. This process continues till no more elements are left in the container.
  4. The final returned result is returned and printed on console.
3.3 Examples
from functools import reduce

lis = [1, 3, 5, 6, 2] 

print (reduce(lambda a,b : a+b,lis)) 
print (reduce(lambda a,b : a if a > b else b,lis)) 
>>>17
>>>6

4-Zip()

4.1 Syntax
zip(*iterators)     # * token means that multi iterators is supported
4.2 Working

zip() returns a single iterator object, having mapped values from all the containers.

4.3 Examples
# 1. How to zip the iterators?
name = [ "Manjeet", "Nikhil", "Shambhavi", "Astha" ] 
roll_no = [ 4, 1, 3, 2 ] 
marks = [ 40, 50, 60, 70 ] 

mapped = list(zip(name, roll_no, marks))
print(mapped)
>>>[('Shambhavi', 3, 60), ('Astha', 2, 70),('Manjeet', 4, 40),('Nikhil', 1, 50)]

# 2. How to unzip?
namz, roll_noz, marksz = zip(*mapped)

# 3. How to traversal them?
players = [ "Sachin", "Sehwag", "Gambhir", "Dravid", "Raina" ] 
scores = [100, 15, 17, 28, 43 ] 

for pl, sc in zip(players, scores): 
    print ("Player :  %s     Score : %d" %(pl, sc))
>>>
Player :  Sachin     Score : 100
Player :  Sehwag     Score : 15
Player :  Gambhir     Score : 17
Player :  Dravid     Score : 28
Player :  Raina     Score : 43

相關文章