Hàm Lambda trong Python là gì?

Trong Python, hàm vô danh là hàm được định nghĩa mà không có tên. Nếu các hàm bình thường được định nghĩa bằng cách sử dụng từ khóa def, thì hàm vô danh được định nghĩa bằng cách sử dụng từ khóa lambda. Vì lý do đó mà hàm vô danh còn được gọi là hàm Lambda.

Cú pháp hàm Lambda trong Python

Một hàm Lambda trong Python có cú pháp sau;

lambda tham_so: bieu_thuc

Hàm Lambda có thể có nhiều tham số nhưng chỉ có 1 biểu thức. Biểu thức sẽ được đánh giá và trả về. Hàm Lambda có thể được sử dụng ở bất cứ nơi nào đối tượng hàm được yêu cầu.

Ví dụ về hàm Lambda trong Python

Đây là ví dụ về hàm Lambda với nhiệm vụ nhân đôi số nhập vào.

nhan_doi = lambda a: a * 2

# Kết quả: 20
# By tinhoccuaem.com
print(nhan_doi(10))

Trong ví dụ này, lambda a: a * 2 là hàm Lambda. a ở đây là tham số và a * 2 là biểu thức (đảm nhiệm việc tính toán và trả về kết quả). Hàm này không có tên, nó trả về một đối tượng hàm – được gán định danh là nhan_doi. Chúng ta có thể gọi nó như một hàm bình thường, lệnh sau:

nhan_doi = lambda a: a * 2

gần giống như:

def nhan_doi(a):
 return a * 2

Sử dụng Lambda trong Python

Thường thì hàm Lambda được sử dụng khi cần một hàm vô danh trong thời gian ngắn, ví dụ như dùng làm đối số cho một hàm bậc cao hơn. Hàm Lambda thường được sử dụng cùng với các hàm Python tích hợp sẵn như filter() hay map(),…

Ví dụ dùng hàm Lambda với filter():

Hàm filter() sẽ lấy các tham số là một hàm và một list. Hàm được gọi với tất cả các mục trong list và list mới sẽ được trả về, chứa các mục mà hàm đánh giá là True. Đây là ví dụ dùng hàm filter() để lọc ra các số chẵn trong list.

list_goc = [10, 9, 8, 7, 6, 1, 2, 3, 4, 5]

list_moi = list(filter(lambda a: (a%2 == 0) , list_goc))

# Kết quả: [10, 8, 6, 2, 4]
# By tinhoccuaem.com
print(list_moi)

Ví dụ dùng hàm Lambda với map():

Hàm map() cũng lấy các tham số là một hàm và một list. Hàm được gọi với tất cả các mục trong list và list mới được trả về chứa các mục được hàm trả về tương ứng cho mỗi mục. Nói thì có vẻ lằng nhằng, bạn xem ví dụ sau sẽ dễ hiểu hơn nhiều:

list_goc = [10, 9, 8, 7, 6, 1, 2, 3, 4, 5]

list_moi = list(map(lambda a: a*2 , list_goc))

# Kết quả: [20, 18, 16, 14, 12, 2, 4, 6, 8, 10]
# By tinhoccuaem.com
print(list_moi)

Còn gì trong phần hàm vô danh mà chúng ta bỏ sót không? Bạn hãy bình luận bên dưới để góp ý và đón chờ bài tiếp theo nhé.

411 BÌNH LUẬN

  1. You ave made some decent points there. I checked on the internet for more information about the issue and found most individuals will go along with your views on this web site.

  2. Merely a smiling visitant here to share the love (:, btw great style and design. Justice is always violent to the party offending, for every man is innocent in his own eyes. by Daniel Defoe.

  3. This is really interesting, You are a very skilled blogger.
    I’ve joined your feed and look forward to seeking
    more of your excellent post. Also, I have shared
    your site in my social networks!

  4. My brother suggested I might like this website. He was entirely right. This post truly made my day. You cann at imagine just how much time I had spent for this information! Thanks!

  5. This is really fascinating, You are a very professional blogger. I ave joined your rss feed and sit up for searching for more of your great post. Also, I have shared your site in my social networks!

  6. Thank you for another fantastic article. Where else could anybody get that kind of info in such an ideal way of writing? I ave a presentation next week, and I am on the look for such info.

  7. Normally I don at learn article on blogs, however I wish to say that this write-up very compelled me to check out and do it! Your writing taste has been amazed me. Thank you, quite nice article.

  8. Nice weblog here! Additionally your site loads up very fast! What host are you using? Can I am getting your affiliate link for your host? I wish my site loaded up as fast as yours lol

  9. This particular blog is really cool as well as diverting. I have discovered a lot of handy tips out of this amazing blog. I ad love to come back again and again. Thanks a lot!

  10. Very nice post and right to the point. I don at know if this is really the best place to ask but do you people have any ideea where to hire some professional writers? Thanks

  11. Thanks for some other magnificent post. Where else may anybody get that kind of info in such a perfect way of writing? I ave a presentation next week, and I am at the search for such info.

  12. Does your blog have a contact page? I’m having problems locating it but, I’d like to send you an email.
    I’ve got some suggestions for your blog you might be interested in hearing.
    Either way, great website and I look forward to seeing it
    develop over time.

  13. Good day! I know this is somewhat off topic but I was wondering which blog
    platform are you using for this site? I’m getting tired of
    Wordpress because I’ve had problems with hackers and I’m looking at options for another platform.
    I would be awesome if you could point me in the direction of a good platform.

  14. I think other web-site proprietors should take this web site as an model, very clean and great user genial style and design, as well as the content. You are an expert in this topic!

  15. Nice blog right here! Also your web site so much up very fast! What host are you the usage of? Can I am getting your associate link on your host? I desire my website loaded up as fast as yours lol

  16. First off I want to say fantastic blog! I had a quick question which I’d like to ask if you don’t mind.
    I was curious to know how you center yourself and clear your mind prior to writing.
    I’ve had a tough time clearing my thoughts in getting my thoughts
    out. I do enjoy writing but it just seems like the
    first 10 to 15 minutes tend to be lost simply just trying
    to figure out how to begin. Any suggestions or hints?
    Appreciate it!

  17. I am extremely inspired together with your writing abilities and also with the structure to your blog.

    Is that this a paid subject or did you customize it yourself?
    Anyway stay up the nice high quality writing, it’s uncommon to peer a great blog like this one today..

  18. Hey there! Quick question that’s totally off topic.
    Do you know how to make your site mobile friendly?
    My site looks weird when viewing from my apple iphone.
    I’m trying to find a theme or plugin that might be able
    to correct this problem. If you have any recommendations, please share.

    With thanks!

  19. Wow! This can be one particular of the most useful blogs We ave ever arrive across on this subject. Basically Excellent. I am also an expert in this topic therefore I can understand your effort.

  20. Wow, marvelous blog format! How long have you ever been running a blog for? you made blogging glance easy. The overall look of your website is magnificent, let alone the content material!

  21. Thanks for every other informative web site.
    Where else may just I get that kind of info written in such a
    perfect manner? I have a venture that I am simply now operating on, and I’ve been on the look out for such information.

  22. Good day! I know this is kinda off topic however , I’d figured I’d ask.

    Would you be interested in trading links or maybe
    guest authoring a blog article or vice-versa? My website addresses a lot of the same
    topics as yours and I believe we could greatly benefit from each other.
    If you might be interested feel free to shoot me an email.
    I look forward to hearing from you! Great blog by the way!

  23. Terrific work! This is the type of information that should be shared around the internet. Shame on Google for not positioning this post higher! Come on over and visit my website. Thanks =)

  24. This is very interesting, You are a very skilled blogger. I have joined your rss feed and look forward to seeking more of your fantastic post. Also, I ave shared your web site in my social networks!

  25. Wow, incredible blog format! How lengthy have you been blogging for? you made running a blog look easy. The total glance of your site is fantastic, as well as the content material!

  26. My brother recommended I might like this blog. He was entirely right. This post actually made my day. You cann at imagine just how much time I had spent for this info! Thanks!

  27. This particular blog is definitely entertaining and diverting. I have found a bunch of useful advices out of this amazing blog. I ad love to go back over and over again. Thanks a lot!

  28. They are added for the price in the house, deducted at the closing and paid directly towards the bank my website for example, if you might be in need for cash

  29. Wow, awesome blog format! How long have you been running a blog for? you make blogging glance easy. The entire glance of your website is magnificent, let alone the content material!

  30. This is very interesting, You are a very skilled blogger. I have joined your rss feed and look forward to seeking more of your great post. Also, I ave shared your site in my social networks!

  31. I’а†ve read many excellent stuff here. Unquestionably worth bookmarking for revisiting. I surprise how a great deal try you set to create this sort of great informative internet site.

  32. You generated some decent points there. I looked on-line for that challenge and identified most people will go coupled with with all of your website.

  33. This is very interesting, You are a very skilled blogger. I ave joined your feed and look forward to seeking more of your magnificent post. Also, I ave shared your site in my social networks!

  34. Your style is really unique compared to other folks I have read stuff from. Many thanks for posting when you ave got the opportunity, Guess I all just book mark this web site.

  35. I was suggested this web site by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my problem. You are wonderful! Thanks!

  36. Tremendous things here. I am very happy to see your article. Thanks a lot and I am taking a look ahead to contact you. Will you kindly drop me a mail?

  37. IaаАа’б‚Т€ТšаЂаŒаАа’б‚Т€ТžаБТžve recently started a site, the info you offer on this site has helped me tremendously. Thank you for all of your time & work.

  38. This is a great tip particularly to those fresh to the blogosphere. Short but very accurate information Appreciate your sharing this one. A must read article!

  39. It’а†s actually a nice and useful piece of info. I’а†m glad that you simply shared this helpful info with us. Please keep us informed like this. Thanks for sharing.

  40. You made some decent points there. I looked on the internet for more information about the issue and found most people will go along with your views on this website.

  41. I just could not leave your site before suggesting that I really enjoyed the usual information an individual provide to your visitors?
    Is gonna be back steadily to check out new posts

THOÁT KHỎI BÌNH LUẬN

Please enter your comment!
Please enter your name here