python(3)-内置函数2

frozenset()    定义一个不能添加修改的集合

>>> s = frozenset()
>>> s.add("aaa")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'frozenset' object has no attribute 'add'

hash()   返回对象的哈希值

>>> a = "abcde"
>>> hash(a)
-1767484571

max()  最大值

>>> max(11,22,33)
33

min()   最小值

>>> min(11,22,33)
11

pow()     幂运算

>>> import math
>>> math.pow(2,3)
8.0

reversed()   反转

>>> a = reversed('abcdef')
>>> for i in a:
...   print(i)
... 
f
e
d
c
b
a

round()    四舍五入

>>> round(3.3)
3
>>> round(3.5)
4

sorted()   排序,还可按照key排序,反转排序

>>> sorted('akfihgke')
['a', 'e', 'f', 'g', 'h', 'i', 'k', 'k']

>>> L = [('b',2),('a',1),('c',3),('d',4)]
>>> sorted(L, key=lambda x:x[1])
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

>>> print(sorted([5,4,6,3,1], reverse=True))
[6, 5, 4, 3, 1]
>>> print(sorted([5,4,6,3,1], reverse=False))
[1, 3, 4, 5, 6]

zip()

>>> x = [1,2,3]
>>> y = [4,5,6]
>>> zipped = zip(x,y)
>>> list(zipped)
[(1, 4), (2, 5), (3, 6)]

文件操作

read()     按字符来读文件

tell()       返回当前指针位置,按字节来算

seek()    设置指针位置

truncate()     获取指针前面的,并删掉后面的,然后保存文件

原文地址:https://www.cnblogs.com/huangxm/p/5191591.html