Python3内置函数(61-69)

版权声明:本文为博主原创文章,未经博主允许不得转载。不准各种形式的复制及盗图 https://blog.csdn.net/qq_26816591/article/details/88558964
# 61.max()
# 返回给定参数的最大值,参数可以为序列。
lst1 = (1, 2, 45, 6, 7, 64, 32, 14)
print(max(lst1))

# 62.memoryview()
# 返回给定参数的内存查看对象
v = memoryview(bytearray('qwerty', 'utf-8'))
print(v[1])
print(v[-1])

# 63.repr()
# 将对象转化为供解释器读取的形式。
str1 = 'hello world'
print(repr(str1))

# 64.reversed()
# 返回一个反转的迭代器。
str2 = 'hello world'
print(list(reversed(str2)))
tuple1 = ('d', 's', 'w', 'q', 'e', 't')
print(list(reversed(tuple1)))

range1 = range(10)
print(list(reversed(range1)))

lst2 = [2, 45, 6, 765, 4, 3, 2, 1, 34, 56, 543, ]
print(list(reversed(lst2)))

# 65.round()
# 返回浮点数x的四舍五入值。
num1 = 12.32
print(round(num1))
num2 = -124.325
print(round(num2))
num3 = 23.2324224
print(round(num3, 3))  # 保留三位小数
num4 = -3279.23378
print(round(num4, 3))  # 保留三位小数

# 66.set()
# 创建一个无序不重复元素集,可进行关系测试,删除重复数据,还可以计算交集、差集、并集等
set1 = set('hello world')
set2 = set('hello python')

print(set1 & set2)
print(set1 | set2)
print(set1 - set2)


# 67.vars()
# 返回对象object的属性和属性值的字典对象。
class A(object):
    a = 1
    b = 'str'


a = A()
print(vars(a))

# 68.zip()
# 将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的对象,这样做的好处是节约了不少的内存。
lst3 = [1, 2, 3]
lst4 = [4, 5, 6, 7]
lst5 = [8, 9, 10]
zip1 = zip(lst3, lst4)
print(list(zip1))

# 69._import_()
# 用于动态加载类和函数 。#
# 如果一个模块经常变化就可以使用 __import__() 来动态载入。
# hello.py
import os

print('hello.py %s' % id(os))

# test.py
__import__('hello') # 加载文件

要点:

  •  max() 返回最大值。
  • memoryview() 给定参数的内存查看对象
  • repr() 转换为对象为解释器读取的形式
  • reversed() 翻转迭代器
  • round() 浮点数四舍五入,可设置保留小数点后几位
  • set() 创建一个无序不重复元素集
  • vars() 返回对象的属性和属性值字典对象
  • zip() 将对象中元素打包成一个个元组
  • _import_() 动态加载类和函数

猜你喜欢

转载自blog.csdn.net/qq_26816591/article/details/88558964