首页 > 编程语言 > 详细

python(day3)-内置函数2

时间:2016-02-16 01:13:15      阅读:164      评论:0      收藏:0      [点我收藏+]

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()     获取指针前面的,并删掉后面的,然后保存文件

python(day3)-内置函数2

原文:http://www.cnblogs.com/huangxm/p/5191591.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!