Introduction to built-in functions in Python3

Source: Internet
Author: User
in Python3, filter, map, and reduce are no longer built-in functions, that is, the <build-in Function>,python3 is class, and the returned result becomes an iterative object

1.filter (function,iterable)

Use the function filter condition to get the data you want in iterable .

From collections Import Iteratortest_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]f = filter (lambda x:x% 3 = = 0, test_list) # Filter Get an iterator print (Isinstance (f, Iterator)) f.__next__ () for I in F:    print (i) #输出True69

  

2.map (function, iterable)

Accepts a function and an iterative object (such as a list, etc.), acting on each element of the sequence sequentially, forming a new iterator.

From collections Import Iteratordef F (x):    return 2 * x  # defines a function t_list = [1, 2, 3, 4, 5]function = Map (f, t_list) PR Int (isinstance (function, Iterator)) # Print (function.__next__ ()) function.__next__ () for I in function:    print (i) # Output True46810

  

3.reduce (function,iterable)

Reduce takes a function in an iterative sequence, which must receive two parameters, and reduce calculates the continuation of the result and the next element of the sequence.

The reduce function does not belong to build-in in Python3, but is under the Functools module and needs to be introduced from the Functools module for use.

From functools Import REDUCEF = reduce (lambda x, Y:x*y, [1, 2, 4]) print (f) #输出8

 

4.hex (x)

Turn a number into 16 binary

>>> Hex ' 0x17 '

  

5.range (stop), range (Start,stop,[step])

To generate an iterative object

>>> from Collections Import iterator>>> isinstance (range (5), Iterator) false>>> from Collections Import iterable>>> isinstance (range (5), iterable) true>>> F.__next__traceback (most Recent call last):  File "<stdin>", line 1, in <module>attributeerror: ' Range ' object have no attribute ' __n Ext__ ' >>> for i in F:     ... Print (i) ... 01234# Range (x) is an iterative object, not an iterator

  

6. Isinstance (object, ClassInfo)

Determine whether a sequence is an iterative object or an iterator

7.list ([iterable])

Convert other sequences to a list

>>> list ((1,2,3,4,5))   #把一个元组转换为一个列表 [1, 2, 3, 4, 5]

  

8.repr (object)

It's no use turning the code into a string object, ignoring it here.

9.slice (stop), slice (start, stop[, step])

Slices of a sequence

>>> a = [1,2,3,4,5,6]>>> a[slice (1,3)][2, 3]>>> a[1:3][2, 3]

  

10.sorted (iterable[, key][, reverse])

>>> sorted ([5,3,2,6,8]) [2, 3, 5, 6, 8]>>> a = {1:5,6:8,3:6}>>> sorted (a)  #默认是按key排序 [1, 3 , 6]>>> Sorted (A.items ())  #按key排序 [(1, 5), (3, 6), (6, 8)]>>> sorted (A.items (), key = Lambda x:x[1])  #按value排序 [(1, 5), (3, 6), (6, 8)]

  

11.reverse ()

Used to reverse the elements in the list, the method does not return a value, but reverses the order of the elements of the list.

A = [1,2,4,5,3,7] A.reverse () a[7, 3, 5, 4, 2, 1]

  

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.