python Advanced Built in Function Usage Example

  • 2021-12-04 10:50:04
  • OfStack

1. enumerate returns an enumerated object for iterative objects of sequence type.

2. eval takes out the contents of the string.

Returns a valid expression in str.

3. exec runs the compiled string.

4. The filter filter filters out the desired object.

Instances


list1 = [1,'ok',3,'kkk']
s = enumerate(list1)
print(s)#<enumerate object at 0x000002D2CC666DB8> Generate 1 Enumeration objects 
for i in s:
    print(i)
#(0, 1)
# (1, 'ok')
# (2, 3)
# (3, 'kkk')
data = list(s)
print(data)#[(0, 1), (1, 'ok'), (2, 3), (3, 'kkk')]
 
a = '1+2+3'
print(a)#1+2+3
print(eval(a))#6
b = 'we s s'
# print(eval(b))# Error reporting, must be a valid expression 
 
c = 12
d = 24
e = 'sum = c+d'
print(e)#sum = c+d
exec(e)
print(sum)#36
 
def func(x):
    return x>5
list2= [i for i in range(10)]
print(func(6))#True
f_list = filter(func,list2)
print(f_list)#<filter object at 0x000001F8E1DFACC8> Return 1 Filter objects 
list3 = list(f_list)
print(list3)#[6, 7, 8, 9]
 
def func1(y):
    if y>5:
        return y*2
list4= [i for i in range(10)]
f_list = filter(func1,list4)
list5 = list(f_list)
print(list5)#[6, 7, 8, 9]
# Only filter, what is filtered out, what is returned, and the value will not be changed 
 
def func2(k):
    if k<5:
        return k# Returns the 1 A bool Value, does not return 0
list6= [i for i in range(10)]
f_list = filter(func2,list6)
list7 = list(f_list)
print(list7)#[1, 2, 3, 4]
 
def demo(x):
    return x*10
 
list8 = [i for i in range(7)]
s = map(demo,list8)# Data must be iterative data 
 
print(s)#<map object at 0x0000029B113BA288> What is generated is 1 A map Object, to view the contents inside, to turn to type 
 
list9 = list(s)
print(list9)#[0, 10, 20, 30, 40, 50, 60]
 
 
 
list10 = ['ok','yes','no']
list11 = [' Yes ',' Yes ',' No ']
 
a = zip(list10,list11)
print(a)#<zip object at 0x000001A4EE89A688> Generate 1 Objects 
print(dict(a))#{'ok': ' Yes ', 'yes': ' Yes ', 'no': ' No '} You can also operate directly 
# Data is manipulated 1 You can no longer operate after the second time 
print(list(a))#[]
# list12 = list(a)
# print(list12)#[('ok', ' Yes '), ('yes', ' Yes '), ('no', ' No ')]
# print(dict(list12))#{'ok': ' Yes ', 'yes': ' Yes ', 'no': ' No '}
# It can be directly converted into a dictionary. If there are objects, they will be paired, and if there are no objects, they will not 

Python Built-in Function List

函数 功能
abs(x) 返回1个数的绝对值。 参数可以是1个整数或浮点数。 如果参数是1个复数,则返回它的模。
all(iterable) 如果 iterable 的所有元素为真(或迭代器为空),返回 True
any(iterable) 如果 iterable 的任1元素为真则返回 True。 如果迭代器为空,返回 False
ascii(object) 返回1个表示对象的字符串
bin(x) 将1个整数转变为1个前缀为“0b”的2进制字符串
bool([x]) 返回1个布尔值,True 或者 False。
breakpoint(*args, **kws) 此函数将您放入调用站点的调试器中
bytearray([source[, encoding[, errors]]]) 返回1个新的 bytes 数组
bytes([source[, encoding[, errors]]]) 返回1个新的“bytes”对象
callable(object) 如果参数 object 是可调用的就返回 True,否则返回 False
char(i) 返回 Unicode 码位为整数 i 的字符的字符串格式
@classmethod 把1个方法封装成类方法
compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1) 将 source 编译成代码或 AST 对象
complex([real[, imag]]) 返回值为 real + imag*1j 的复数,或将字符串或数字转换为复数
delattr(object, name) 如果对象允许,该函数将删除指定的属性
dict(**kwarg) dict(mapping, **kwarg) dict(iterable, **kwarg) 创建1个新的字典
dir([object]) 如果没有实参,则返回当前本地作用域中的名称列表。如果有实参,它会尝试返回该对象的有效属性列表
divmod(a, b) 它将两个(非复数)数字作为实参,并在执行整数除法时返回1对商和余数
enumerate(iterable, start=0) 返回1个枚举对象
eval(expression[, globals[, locals]]) 返回值就是表达式的求值结果
exec(object[, globals[, locals]]) object 必须是字符串或者代码对象。如果是字符串,那么该字符串将被解析为1系列 Python 语句并执行(除非发生语法错误)。如果是代码对象,它将被直接执行
filter(function, iterable) 过滤序列,过滤掉iterable不符合条件的元素,function为条件,返回由符合条件元素组成的新列表
float([x]) 返回从数字或字符串 x 生成的浮点数
format(value[, format_spec]) 将 value 转换为 format_spec 控制的“格式化”表示
frozenset([iterable]) 返回1个新的 frozenset 对象,它包含可选参数 iterable 中的元素
getattr(object, name[, default]) 返回对象命名属性的值
globals() 返回表示当前全局符号表的字典
hasattr(object, name) 如果字符串是对象的属性之1的名称,则返回 True,否则返回 False
hash(object) 返回该对象的哈希值(如果它有的话)
help([object]) 启动内置的帮助系统
hex(x) 将整数转换为以“0x”为前缀的小写106进制字符串
id(object) 返回对象的“标识值”
input([prompt]) 接受1个标准输入数据
int([x]) int(x, base=10) 返回1个基于数字或字符串 x 构造的整数对象,或者在未给出参数时返回 0
isinstance(object, classinfo) 如果参数 object 是参数 classinfo 的实例或者是其 (直接、间接或 虚拟) 子类则返回 True。 如果 object 不是给定类型的对象,函数将总是返回 False
issubclass(class, classinfo) 如果 class 是 classinfo 的 (直接、间接或 虚拟) 子类则返回 True
iter(object[, sentinel]) 返回1个 iterator(迭代器) 对象
len(s) 返回对象的长度(元素个数)
list([iterable]) 将可迭代对象(字符串、列表、元祖、字典)转换为列表
locals() 更新并返回表示当前本地符号表的字典
map(function, iterable, …) 根据提供的函数对指定序列做映射
max(iterable, *[, key, default]) max(arg1, arg2, *args[, key]) 返回可迭代对象中最大的元素,或者返回两个及以上实参中最大的
memoryview(obj) 返回由给定实参创建的“内存视图”对象
min(iterable, *[, key, default]) min(arg1, arg2, *args[, key]) 返回可迭代对象中最小的元素,或者返回两个及以上实参中最小的
next(iterator[, default]) 通过调用 iterator 的 __next__() 方法获取下1个元素。如果迭代器耗尽,则返回给定的 default,如果没有默认值则触发 StopIteration
object 返回1个没有特征的新对象, object 是所有类的基类
oct(x) 将1个整数转变为1个前缀为“0o”的8进制字符串
open(file, mode=‘r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None) 打开 file 并返回对应的 file object。如果该文件不能打开,则触发 OSError
ord© 对表示单个 Unicode 字符的字符串,返回代表它 Unicode 码点的整数
pow(base, exp[, mod]) 返回 base 的 exp 次幂;如果 mod 存在,则返回 base 的 exp 次幂对 mod 取余
print(*objects, sep=' ‘, end='\n', file=sys.stdout, flush=False) 将 objects 打印到 file 指定的文本流,以 sep 分隔并在末尾加上 end
property(fget=None, fset=None, fdel=None, doc=None) 返回 property 属性
range(stop) range(start, stop[, step]) 返回不可变的序列
repr(object) 返回包含1个对象的可打印表示形式的字符串
reversed(seq) 返回1个反向的 iterator(迭代器)
round(number[, ndigits]) 返回 number 舍入到小数点后 ndigits 位精度的值
set([iterable]) 返回1个新的 set 对象,可以选择带有从 iterable 获取的元素
setattr(object, name, value) 设置属性值
slice(stop) slice(start, stop[, step]) 返回1个表示由 range(start, stop, step) 所指定索引集的 slice 对象
sorted(iterable, *, key=None, reverse=False) 根据 iterable 中的项返回1个新的已排序列表
@staticmethod 将方法转换为静态方法
str(object='') str(object=b'', encoding=‘utf-8', errors=‘strict') 返回1个 str 版本的 object
sum(iterable, /, start=0) 从 start 开始自左向右对 iterable 的项求和并返回总计值
super([type[, object-or-type]]) 调用父类(超类)
tuple([iterable]) 返回1个不可变的序列
type(object) type(name, bases, dict) 传入1个参数时,返回 object 的类型
vars([object]) 返回模块、类、实例或任何其它具有 __dict__ 属性的对象的 __dict__ 属性
zip(*iterables) 创建1个聚合了来自每个可迭代对象中的元素的迭代器
__import__(name, globals=None, locals=None, fromlist=(), level=0) 动态加载类和函数

The above is the python advanced built-in function usage example details, more about python which advanced built-in function information please pay attention to other related articles on this site!


Related articles: