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   ZhangZhihuiAAA  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2023-07-31 Go - go.work, go.mod, go.sum
2023-07-31 Go - installation
2023-07-31 Go - env
2023-07-31 Python - match case
 
点击右上角即可分享
微信分享提示