Interesting functions in python and interesting functions in python

Source: Internet
Author: User

Interesting functions in python and interesting functions in python

Filter (function, sequence): Execute function (item) in sequence for items in sequence, and combine items whose execution result is True into a List/String/Tuple (depending on the sequence type) return: >>> def f (x): return x % 2! = 0 and x % 3! = 0 >>> filter (f, range (2, 25) [5, 7, 11, 13, 17, 19, 23] >>> def f (x ): return x! = 'A'> filter (f, "abcdef") 'bcdef 'map (function, sequence): Execute function (item) in sequence for items in sequence ), see the execution result to form a List and return: >>> def cube (x): return x * x >>>> map (cube, range (1, 11) [1, 8, 27, 64,125,216,343,512,729,100 0] >>> def cube (x): return x + x... >>> map (cube, "abcde") ['A', 'bb ', 'cc', 'dd', 'ee'] In addition, map supports multiple sequence, this requires the function to support the corresponding number of parameter inputs: >>> def add (x, y): return x + y >>>> map (add, range (8 ), range (8) [0, 2, 4, 6, 8, 10, 12, 14] reduce (function, sequence, starting_value): calls the function sequentially for the items in sequence, if starting_value exists, it can also be called as an initial value. For example, it can be used to sum the List: >>> def add (x, y): return x + y >>> reduce (add, range (1, 11) 55 (Note: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10) >>> reduce (add, range (1, 11), 20) 75 (Note: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 20)

  


How to define functions in python

Use the def keyword, which contains the parameter list in brackets.
Def add (a, B ):
Return a + B

# Call
Print add (1, 2)

What is the zip () function in python?

Zip ([1, 2, 3], ['A', 'B', 'C'])
The result is
[(1, 'A'), (2, 'B'), (3, 'C')]

Is to extract the elements of each array in sequence, and then combine
Operands can be more
Zip ([, 3], ['A', 'B', 'C'], [, 6 ])

If the element length is inconsistent, it will be cut to the same length.
In addition, zip (* list) is an asterisk in front of the array, which is the inverse operation of the above operation.
The result of zip (* [(1, 'A'), (2, 'B'), (3, 'C')]) is [1, 2, 3], ['A', 'B', 'C']

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.