Python learning--max and reduce functions

Source: Internet
Author: User

Question: I have a list of these:

1
[' a.b.c.d11u.e.f.g ', ' e.f88.g ', ' caa3.z.brr ', ' z.48.ff.ee ']

Need to find the most node (between nodes by. Split)

Seemingly simple work, to do it in a pythonic way, or to some degree of Python's built-in functions, such as the most familiar with the max, but you will use it is not commonly used optional parameters: key

1
2
Node_list = [' a.b.c.d11u.e.f.g ', ' e.f88.g ', ' caa3.z.brr ', ' z.48.ff.ee ']
max_node = max (Node_list, Key=lambda N: N.count ('. '))

Here, the method of using the key parameter to change the max comparison list element achieves the goal of accomplishing the task.

By the way, another one is not a lot of built-in functions: reduce, use it to solve this problem

1
2
Node_list = [' a.b.c.d11u.e.f.g ', ' e.f88.g ', ' caa3.z.brr ', ' z.48.ff.ee '

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.