0. 阅读目录
1. LIST
the_list = [] #新建一个空列表
the_list[i] = x #指定列表中i位置项的值
the_list[s:] #提取从s位置开始的子列表
the_list[s:t+1] #提取从s到t位置的子列表
del the_list[i] #删除列表中项【可删除子列表】
the_list = [x for i in range(n)] or [x]*n #用固定值初始化列表(x = 0; n = 10)
the_list = range(1,t+1, dist) #用连续值初始化列表(s = 0; t = 10; dist = 2); dist为间隔值
the_list.append(x) or the_list.insert(x,len(the_list)) #在表末端追加项
the_list.pop() or the_list.pop(len(the_list)-1) #返回并删除最后一个项
the_list.remove(x) #删除第一次出现的项x 【x必须存在于列表中,否则异常】
the_list.count(x) #返回项x在列表中出现的次数
the_list.index(x) #返回项x第一次出现的位置 【x必须存在于列表中,否则异常】
the_list.extend(l_2) or the_list + l_2 #合并l_2到原列表上
the_list.sort() #排序
the_list.reverse() #倒序
l_after = [ <exp1> for item in list if <exp2> ] # list的递推式构造集合(list comprehension)
l_1 = l_0 #别名;增加一个指针l_1到l_0所指向的地址,在任一指针上的操作反应到其他指针上【复制指针地址】
l_1 = l_0[:] #克隆;l_1为l_0的拷贝,在l_0上的操作不影响l_1【复制内容】
list与string互相转换:
str_0 = 'abc'
list_0 = list(str_0)
str_1 = "".join(list)
2. DICTIONARY
#KEY:To be used as a dictionary key, an object must support the hash function (e.g. through __hash__), equality comparison (e.g. through __eq__ or __cmp__), and must satisfy the correctness condition above. 【list type cannot be used as keys because it is unhashable.】
dict = {} #新建一个新字典【*字典无序】
dict = {'key0':'item0', 'key1':'item1'} #每个字典元素包含KEY【integer/string】和VALUE。
dict[key0] #提取指定KEY的对应VALUE值【key0必须存在于字典中,否则异常】
dict.get(key0,x) #提取指定KEY的对应VALUE值【如果key0不存在于字典中,则返回x】
dict.update(dict2) #合并dict2到dict
dict.popitem() #返回并删除第一个项【字典不能为空,否则异常】
dict.pop('key0') #返回指定KEY所对应的值并删除整个项【key0必须存在于字典中,否则异常】
dict.has_key(key0) #返回是否含指定KEY的Boolean值
dict.keys() #返回字典所有KEY的列表
dict.values() #返回字典所有VALUE的列表
dict.items() #返回字典所有(KEY, VALUE)的列表
dict.clear() #清空字典
del dict #删除字典
dict2 = dict #别名
dict2 = dict.copy() #克隆
cmp(dict, dict2) #比较字典,返回1,-1,0【优先级:元素个数,键大小,键值大小】
3. TUPLE
tup = ('a','b','c') #新建一个元组/常量数组
tup[i] #提取列表中i位置项的值【不可修改】
tup+tup2 #返回两个常量数组的合并值
tup.count(x) #返回项x在常量数组中出现的次数
tup.index(x) #返回项x第一次出现的位置 【x必须存在于常量数组中,否则异常】
【常量数组不可修改,无法append, pop, remove, sort, reverse】
4. STRING
str = '' #新建一个空字符串
str[s:t] #提取从s到t位置的子字符串
substring (not) in str #判断操作
str.startswith(substring[, index]) #从index(default: 0)的位置开始检查substring是否在str的开始
str.find(substring, [s [, t]]) #在指定s,t的范围内查找子字符串,并返回其第一次出现的位置,若不存在,返回-1
str.rfind(substring, [s [, t]]) #在指定s,t的范围内查找子字符串,并返回其最后一次出现的位置,若不存在,返回-1
str.index(substring, [s [, t]]) #同find【子字符串必须存在于字符串中,否则异常】
str.rindex(substring, [s [, t]]) #同rfind【子字符串必须存在于字符串中,否则异常】
str.count(substring, [s [, t]]) #返回子字符串在字符串中出现的次数
str1 = str.capitalize() #返回首字母大写的克隆
str1 = str.lower() #返回转小写
str1 = str.upper() #返回转大写
str1 = str.swapcase() #返回大小写互换
list1 = str.split([sep [, max_split-1]]) #返回以sep切分字符串的列表,共max_split个【不包含sep】
list1 = str.rsplit([sep [, max_split-1]]) #同上,除了切分时顺序从右开始
str1 = str.strip([, chars]) #返回切掉两侧chars(default: whitespace)的字符串
str1 = str.lstrip([, chars]) #同上,除了只切掉左侧chars
str1 = str.rstrip([, chars]) #同上,除了只切掉右侧chars
str1 = str.ljust(str, width[, fillchar]) #在右侧添加足够的fillchar(default: whitespace),直到width==len(str1)
str1 = str.rjust(str, width[, fillchar]) #同上,除了在左侧添加
str1 = str.center(str, width[, fillchar]) #同上,除了在两侧添加
str1 = str.zfill(str, width) #在左侧添加‘0’直到width==len(str1)【如果‘+-’符号在最左边,保留其位置】
str1 = str.replace(old_str, new_str[, max_replacements]) #用new_str替换str中的old_str,直到达到max_replacements
STRING FUNCTIONS 【needs 'import string' first】
string.capwords(str [, sep]) #依照sep(default: whitespace)分隔(split)字符串【保留sep】,首字母大写(capitalize),最后合并(join)
trans_table = string.maketrans(from_str, to_str) #返回一个char对应char的翻译表 【from_str, to_str 须有同样长度】
string.translate(str, trans_table) #根据翻译表返回str的翻译
【Functions above expressed as str.func(paras) could be used by calling string.func(str, paras), after importing the string module.】
5. REFERENCE
http://www.cnblogs.com/zhengyuxin/articles/1938300.html (Python list 操作)
http://www.cnblogs.com/BeginMan/archive/2013/03/21/2972857.html (Python零碎知识)
https://wiki.python.org/moin/DictionaryKeys (Python Wiki)
https://docs.python.org/2/library/ (The Python Standard Library)