python 胡乱小结

来源:互联网 发布:php 敏感词过滤 编辑:程序博客网 时间:2024/04/30 05:39
import
math
os
sys
random
string
re - 正则表达式
codecs
unicodedata
array
"copy-浅拷贝、深拷贝
copy()
deepcopy()"
"time
time.clock()"

-------------------------------------------------------------------------
Error
"expect Error , e :
print e,"
IOError
UnicodeError
ValueError
EOFError
IndexError
KeyboardInterrupt

-------------------------------------------------------------------------
python    "# -*- coding: UTF-8 -*-
"
print a,    #! usr/bin/env python
raw_input("hello :")    
id(obj)    
dir(list)/dir([])    
"if __name__ == '__main__':
"    
cmp()    
str()    
type()    
int()    float()
bool()    
abs()    
pow()    
round()    四舍五入
hex()    
oct()    
ord('a')    ASCII
chr(97)    ASCII
unichr(97)    
pass    
raise    
as    通常和with结合使用,或者别名
yield    生成器,通常定义在函数中,用for来调用该函数
lamda    没有函数名的函数,更关注返回结果
random()    0-1  randint(1,99)
seq[1:9:-2]    步长,seq[:0]=''
seq*2    
len()    
range(min,max,step)    
list(iter)    拷贝对象索引/重新建立对象
str()-不建议采用    unicode()/decode()/encode()
chr()-不建议采用    unichr()
tuple()    元组
max()/min()    
reversed()    
sorted()    
sum()    
string.uppercase    
string.lowercase    
string.letters    
string.digits    
string.find(str,beg,end)    
string.count(str,beg,end)    
string.join(seq)    
string.replace(str1,str2,num)    str1替换成str2
string.split(str,num)    
string.strip([obj])    删除字符串左边和末尾的空格
    
while i < len(mystring):    "length = len(mystring)
while i < length:"
%c,%r,%s,%d,%u,%o,%x,%e,%f,%%     '%.2f' % 123.456
re.search()    
re.compile()    
re.match()    
re.sub()    
f = open(r'C:\temp\readme.txt', 'r')    原始操作符
"u'abc'
unicode()/decode()/encode()
unichr()"    "unicode操作符
程序中出现字符串时一定要加个前缀u
不要用str(),用unicode()替代
不要用过时的string模块
不到必须时不要在程序里面编解码Unicode字符"
字符串不以NUL或'\0'结束    
字符串排序是字典序(大写>小写>数字),不是字母序    
list(obj)    
list.append(obj)    
list.extend(seq)    
list.count(obj)    
list.index(obj)    
list.insert(index,obj)    
list.pop(index)    
list.remove(obj)    
list.reverse()-直接改变原list    reversed()-返回新的对象
list.sort()    sorted() 排序算法是归并排序(timsort)的衍生算法,时间复杂度O(lg(n!))
列表的数据结构:    
堆栈:LIFO(餐厅的一摞盘子)    append(),pop()-弹出最后一个
队列:FIFO(排队交钱)    pop(0)-弹出第一个
容器包括列表、元组    
tuple()    
"哈希表:
是一种数据结构,键值对
获取键
哈希函数计算结果
选择某个地址存储该结果
特点:无序,快速"    
dict() - 工厂函数    
dict.clear()    
dict.pop('name')    
cmp()-字典长度>键>值    
dict1.update(dict2)    将dict2添加到dict1中
dict.keys()    
dict.values()    
可变集合set - add/update/remove/discard/pop/clear    set('name')
不可变集合frozenset    
错误:语法错误 + 逻辑错误    
解释器检测到错误后,抛出异常    系统错误、硬件中断
try - except - finally    
expect Exception, e:    
expect BaseException, e: 最高层级    sys.exc_info()放在expect语句中
过程是简单、特殊、没有返回值的函数    
如果函数返回多个对象,则聚集起来并以一个元组返回    

@TEST

装饰器是函数    

星号操作符之后的形参作为元组传递给函数,如果没有视为空    可变长度的参数-元组*()
双星号操作符之后的形参作为字典传递给函数,如果没有视为空    可变长度的参数-字典**{key:value}
元组与字典可共存,元组必须放在前面   (*tuple1, **dict1)
函数式编程:lamda(匿名函数)/filter/map/reduce    
filter(lamda n:n%2, [1,2,3,4,5,6])    [1,3,5]
map(lamda x,y:x+y,[1,3,5],[2,4,6])    [3,7,11]
reduce((lamda x,y:x+y),range(5))    10
先从局部作用域开始搜索,再搜索全局域-global    
函数嵌套可以共用内部变量    
"闭包是函数:一个内部函数,引用外部作用域(非全局作用域)的变量。
自由变量:上述的那个变量。
回调是函数。"    
"生成器是一个带yield语句的函数。
生成器是挂起返回出中间值并多次继续的协同程序。
yield.next()/send(num)/close()"    from _future_ import generators
"一个名称空间就是一个从名称到对象的关系映射集合。
局部名称空间、全局名称空间、内建名称空间
解释器首先加载内建名称空间>全局>局部
__builtins__模块:内建名称空间中内建名字的集合,包含
__builtin__模块的所有名字。"    "globals().keys()
locals().keys()"
"搜索路径:查找某个文件的操作
路径搜索:查找一组目录"    "PYTHONPATH
sys.path.append('///')"
reload(module)    
包是一个有层次的文件目录结构。