Python tips
1、xrange
Docstring:
xrange(stop) -> xrange object
xrange(start, stop[, step]) -> xrange object
Like range(), but instead of returning a list, returns an object that
generates the numbers in the range on demand. For looping, this is
slightly faster than range() and more memory efficient.
2、int自动转换为long
In [1]: import sys In [2]: sys.version Out[2]: '2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)]' In [3]: a = sys.maxint b = a + 1 #超出int限制,自动转换为long类型 type(a),type(b) Out[3]: (int, long)
注:Python里的长整型所能表达的范围远远超过C语言中的长整型,事实上,Python长整型仅受限于用户计算机的虚拟内存,类似于java中的BigInter类型,从Python2.3开始,不存在报整型溢出错误,结果会自动转换为长整型。
3、删除列表中的重复元素
>>> A = [1,2,3,4,2,3,4,1,2] >>> B = ['a','b','c','a','b','c'] >>> list(set(A)),list(set(B)) ([1, 2, 3, 4], ['a', 'c', 'b']) >>> _[0] #下划线 _ 在解释器中有特别的含义,表示最后一个表达式的值 [1, 2, 3, 4]
4、删除字符串中的空格
In [1]: s = ' Hello World ' In [2]: s.lstrip() #删除左边空格 Out[2]: 'Hello World ' In [3]: s.rstrip() #删除右边空格 Out[3]: ' Hello World' In [4]: s.strip() #删除字符串首尾空格 Out[4]: 'Hello World' In [5]: s.split() Out[5]: ['Hello', 'World'] In [6]: ''.join(s.split()) #删除字符串中所有空格 Out[6]: 'HelloWorld'
5、列表转换为字典,value为列表元素,key为列表元素对应的序号
In [1]: a = ['a','b','c'] c = list(enumerate(a)) d = [(n,a[n]) for n in xrange(len(a))] In [2]: dict(c) Out[2]: {0: 'a', 1: 'b', 2: 'c'} In [3]: dict(d) Out[3]: {0: 'a', 1: 'b', 2: 'c'}
6、re.search() vs re.match()
Python offers two different primitive operations based on regular expressions: re.match() checks for a match only at the beginning of the string, while re.search() checks for a match anywhere in the string (this is what Perl does by default).
>>> re.match("c", "abcdef") # No match >>> re.search("c", "abcdef") # Match <_sre.SRE_Match object at ...>
Regular expressions beginning with '^' can be used with search() to restrict the match at the beginning of the string:
>>> re.match("c", "abcdef") # No match >>> re.search("^c", "abcdef") # No match >>> re.search("^a", "abcdef") # Match <_sre.SRE_Match object at ...>
Note however that in MULTILINE mode match() only matches at the beginning of the string, whereas using search() with a regular expression beginning with '^' will match at the beginning of each line.
>>> re.match('X', 'A\nB\nX', re.MULTILINE) # No match >>> re.search('^X', 'A\nB\nX', re.MULTILINE) # Match <_sre.SRE_Match object at ...>
7、计算字符串之间的汉明距离
In [1]: def hamming_distance(s1, s2): if len(s1) != len(s2): raise ValueError("Undefined for sequence of unequal length") return sum([ch1 != ch2 for ch1, ch2 in zip(s1, s2)]) In [2]: s1 = 'Hello Ronaldinho' s2 = 'Hallo Ronalhuang' hamming_distance(s1,s2) Out[2]: 6
8、由rand7构造rand10
rand7()返回1~7随机自然数,利用rand7构造rand10()随机返回1~10
import random def rand7(): return random.randrange(1,7) def rand10(): temp = (rand7()-1)*7 + rand7() #构造出1~49上的均匀分布 while temp > 40: #如果temp>10,则有39/49的概率需要循环,容易死循环,效率降低 temp = (rand7()-1)*7 + rand7() return temp%10+1
9、二维数组转置
[[r[col] for r in arr] for col in range(len(arr[0]))]
10、pass