Python3內置函數(61-69)

# 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_() 動態加載類和函數

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章