Python common functions

Source: Internet
Author: User

1. Range and Xrange

Range ([Start,] stop[, step])

Range can create a list from start to stop (not included), and when used in loops, loads the list into memory at once.

Xrange ([Start,] stop[, step])

Xrange can create a generator from start to stop (not included), which, when used in a loop, does not load everything at once, but instead returns one value per call, so it always consumes very little memory and does not require significant memory space.

2. Lambda

Lambda [arg1[, arg2, ... ArgN]: expression

Lambda is an anonymous function in Python.

Func =  lambda x,y:x*yfunc (#Ten
3. Map

Map (function, sequence): Executes function (item) on item in sequence, returns the list

Map (Lambda x:x+1, range (1,5))#[2, 3, 4, 5]
4. Filter

Filter (function, sequence): Executes function (item) on item in sequence, and the item that executes the result of true is composed of a list/string/ Tuple (depending on the type of sequence)

def func (x)     return x>5Filter (func, Range (#[6, 7]
5. Reduce

Reduce (function, sequence, starting_value): Invokes function for the item order in the sequence and, if there is a starting_value, can also be called as an initial value

Reduce (lambda x, Y:x+y, Range (1, 101))   #5050#  1+2+3+...+100 = 5050 reduce (lambda x, Y:x+y, Range (1, 101),   # 5150 # + 1+2+3+...+100 = 5150

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.