Home  >  Article  >  Backend Development  >  Introduction to built-in functions in python3

Introduction to built-in functions in python3

零下一度
零下一度Original
2017-07-23 11:29:381153browse

In python3, filter, map, and reduce are no longer built-in functions, that is, ce285a72c216c48fa20429d6ce19a64f. In python3, they are classes, and the return results become iterable objects

1.filter(function,iterable)

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

from collections import Iterator
test_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
f = filter(lambda x: x % 3 == 0, test_list)
# filter 得到的是一个迭代器

print(isinstance(f, Iterator))
f.__next__()

for i in f:
    print(i)

#输出

True
6
9

 

2.map(function, iterable)

Accepts a function and an iterable object (such as a list, etc.), and applies the function to each element of the sequence in turn to form a new iterator.

from collections import Iterator


def f(x):
    return 2 * x  # 定义一个函数

t_list = [1, 2, 3, 4, 5]

function = map(f, t_list)

print(isinstance(function, Iterator))
# print(function.__next__())
function.__next__()
for i in function:
    print(i)

#输出
True
4
6
8
10

 

3.reduce(function,iterable)

reduce applies a function to an iterable sequence. This function must receive two Parameters, reduce continues the result and performs cumulative calculations with the next element of the sequence

The reduce function no longer belongs to the build-in in python3, but under the functools module. If you need to use it, you need to start from Introduced in the functools module

from functools import reduce

f = reduce(lambda x, y: x*y, [1, 2, 4])

print(f)

#输出
8

 

4.hex(x)

Convert a number to hexadecimal

>>> hex(23)
'0x17'

 

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

Generate an iterable 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: &#39;range&#39; object has no attribute &#39;__next__&#39;
>>> for i in f:
...     print(i)
...
0
1
2
3
4

# range(x) 是一个可迭代对象,而不是一个迭代器

 

6. isinstance(object, classinfo)

Determine whether a sequence is an iterable object or iterator

7 .list([iterable])

Convert other sequences into a list

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

 

8.repr(object )

Convert the code into a string object, it is of no use, ignore it here

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

Slice of 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()

is used in reverse lists Element, this method has no return value, but will sort the elements of the list in reverse order.

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

 

The above is the detailed content of Introduction to built-in functions in python3. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn