ZhangZhihui's Blog  

>>> list(map(lambda x, y: x * y, [1,2,3,4], [5,6,7,8]))

[5, 12, 21, 32]

Here we have called the map function and sent a lambda expression as first argument. Instead of the lambda function, you can send operator.mul.

>>> list(map(operator.mul, [1,2,3,4], [5,6,7,8]))

[5, 12, 21, 32]

The operator module has two more useful functions that we can use instead of lambda functions while doing functional programming. These are itemgetter and attrgetter functions. The itemgetter function can be used to get items from sequences and attrgetter can be used to extract attributes from objects. Let us see how to use these functions:

>>> from operator import itemgetter

>>> employees = [ ('Rajendra', 'Kumar', 32, 6000),

...  ('Sam', 'Saxena', 43, 8000),

... ('Shyamchandra', 'Verma', 23, 3000),

... ('Sam', 'Gupta', 33, 7000),

... ('Sam', 'Sung', 31, 5000)

... ]

We imported the itemgetter function from the operator module. We have a list of tuples, which we used earlier when learning about sorted function. To sort this list of tuples based on element at the second index, we would write this:

>>> sorted(employees, key=lambda t: t[2])

[('Shyamchandra', 'Verma', 23, 3000), ('Sam', 'Sung', 31, 5000), ('Rajendra', 'Kumar', 32, 6000), ('Sam', 'Gupta', 33, 7000), ('Sam', 'Saxena', 43, 8000)]

Instead of the lambda function, we can use the itemgetter function which is more readable and faster and gives us the same result.

>>> sorted(employees, key=itemgetter(2))

[('Shyamchandra', 'Verma', 23, 3000), ('Sam', 'Sung', 31, 5000), ('Rajendra', 'Kumar', 32, 6000), ('Sam', 'Gupta', 33, 7000), ('Sam', 'Saxena', 43, 8000)]

We can do multiple levels of sorting by sending more than one index values to the itemgetter function.

>>> sorted(employees, key=itemgetter (1,2))

[('Sam', 'Gupta', 33, 7000), ('Rajendra', 'Kumar', 32, 6000), ('Sam', 'Saxena', 43, 8000), ('Sam', 'Sung', 31, 5000), ('Shyamchandra', 'Verma', 23, 3000)]

The sorting is done first by index 1 element and then by index 2 element.

The next example shows how to use the attrgetter function.

from operator import attrgetter


class Student:
    def __init__(self, name, marks, birthYear):
        self.name = name  
        self.marks = marks
        self.birthYear = birthYear

    def __str__(self):
        return f'{self.name} {self.marks} {self.birthYear}'


s1 = Student('John', 97, 1988)
s2 = Student('Sam', 89, 1987)
s3 = Student('Pam', 99, 1982)
s4 = Student('Pam', 99, 1978)
L = [s1, s2, s3, s4]
L.sort(key=attrgetter('marks')) 

for i in L:
    print(i)

print()
L.sort(key=attrgetter('marks', 'birthYear')) 

for i in L:
    print(i)

Output-

Sam 89 1987

John 97 1988

Pam 99 1982

Pam 99 1978

Sam 89 1987

John 97 1988

Pam 99 1978

Pam 99 1982

For the key parameter of the sort method, we have called the attrgetter function with the marks attribute. So, the list will be sorted based on the marks attribute. We can have multiple levels of sorting; in the next call we have sent two strings, so first it will sort by marks and then by birthYear.

 

posted on 2024-07-31 20:13  ZhangZhihuiAAA  阅读(6)  评论(0编辑  收藏  举报