这篇文章是楼主在刷leetcode的时候逐渐摸索出来的,可以让大家的python代码更加简洁清晰,面试工作的时候能更好理清思路~如何优雅地copy 一个 list? arr[:]
如何遍历字典?dict.items()
如何将任何事物转换成迭代器?iter(pp2fr.keys()))
如何定义最大值?import sys; sys.maxsize
在判断两个浮点数 a 和 b 是否相等时,不要用 a==b,应该判断二者之差的绝对值 abs(f1 - f2) <= allowed_error。
使用 reserve 来避免不必要的重新分配
如果序列中存在的元素数量少于你要的,例如,你请求 a[:-2] 但是 a 只有一个元素,你会得到一个空列表,而不是一个错误.有时候你或许希望返回的是一个错误,所以你必须知道这点
XOR is its own inverse to find the missing element in linear time.
ordered dictionary 的所有操作都是常数时间
bool的更新 changed |= check(i)
random import random; print(random.random())
判断浮点数整数 num % 1 ==1
dict.pop(k,d) 其中d是k找不到时的默认值
判断两个数的符号 sig = (a < 0) == (b < 0)
直观的不等式 print(1 > 1 > 0)
sum(i == j for i, j in zip(secret, guess)) 判断有多少符合条件
看到除法就想到对0的特殊处理
随机抽取变量 random.choice(arr)
判断二维矩阵的有效性 if not any(board): return
生成交换序列 itertools.combinations(range(W), 2)
多使用 dict.get(),这样找不到的话返回None而不是报错
在搜索问题中,优雅地遍历当前节点的邻居for i in range(x-1, x+2) for j in range(y-1,y+2)
如何设置max函数的默认值(这点在arr为空的时候特别有用)?max_num = max(arr, default=0)
基本类型
对整数
round(x[, n])
math.floor(x)
math.ceil(x)
对浮点数
(-2.0).is_integer()
对二进制python convert into 32 bin '{:032b}'
获取最大位数: t.bit_length()
>>> n = -37
>>> bin(n)
'-0b100101'
>>> n.bit_length()
6
字符串
str.index(sub[, start[, end]])¶
str.isalnum()
str.isalpha()
str.isdigit()
str.isupper()
str.islower()
str.isspace()
str.endswith(suffix[, start[, end]])
str.center(width[, fillchar])
str.count(sub[, start[, end]])
str.ljust(width[, fillchar])
str.lstrip([chars])
str.rfind(sub[, start[, end]])
str.rindex(sub[, start[, end]])
str.rjust(width[, fillchar])
str.lower()
str.upper()
str.partition(sep)
str.rpartition(sep)
str.swapcase()
# Split the string at the first occurrence of sep, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing the string itself, followed by two empty strings.
>>> ' spacious '.lstrip()
'spacious '
>>> 'www.example.com'.lstrip('cmowz.')
'example.com'
统计奇偶个数
chips=[n%2 for n in chips]
n1,n2=chips.count(0),chips.count(1)
return min(n1,n2)
queue (heapq)
基本操作
import heapq
q = []
for i in range(5):
heapq.heapq_push
while not q.empty():
print q.get()
top k freq element
heapq.nlargest(k, count.keys(), key=count.get)
Deque
from collections import deque
d = deque('ghi') # make a new deque with three items
d.append('j') # add a new entry to the right side
d.appendleft('f') # add a new entry to the left side
# deque(['f', 'g', 'h', 'i', 'j'])
d.pop() # return and remove the rightmost item
# 'j'
d.popleft() # return and remove the leftmost item
# 'f'
list(d) # list the contents of the deque
# ['g', 'h', 'i']
d[0] # peek at leftmost item
# 'g'
d[-1] # peek at rightmost item
# 'i'
list(reversed(d)) # list the contents of a deque in reverse
['i', 'h', 'g']
'h' in d # search the deque
True
d.extend('jkl') # add multiple elements at once
# deque(['g', 'h', 'i', 'j', 'k', 'l'])
d.rotate(1) # right rotation
# deque(['l', 'g', 'h', 'i', 'j', 'k'])
d.rotate(-1) # left rotation
# deque(['g', 'h', 'i', 'j', 'k', 'l'])
d.clear() # empty the deque
d.extendleft('abc') # extendleft() reverses the input order
# deque(['c', 'b', 'a'])
List
展开 list
import itertools.chain.from_iterable
Contents = itertools.chain.from_iterable(list(map(filemap, fileslist)))
range 中第二个元素比第一个元素小
这种情况没有任何输出
>>> for i in range(0,-2):
... print(i)
判断一个列表为空得最佳实践
if not a:
print "List is empty"
#不要用len(a)来判断
如何合并两个列表
1.不考虑顺序(原来问题不是很明确)
listone + listtwo
#linstone.extend(listtwo)也行,就是会修改listone
Python 中如何复制一个列表
可以用切片的方法
new_list = old_list[:]
# or
new_list = list(old_list)
可以使用 copy.copy(),比 list()稍慢,因为它首先去查询 old_list 的数据类型
如何利用下标从列表中删除一个元素
1.del
a = range(10)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
del a[-1]
# [0, 1, 2, 3, 4, 5, 6, 7, 8]
2.pop
a = ['a', 'b', 'c', 'd']
a.pop(1)
# now a is ['a', 'c', 'd']
a = ['a', 'b', 'c', 'd']
a.pop()
# now a is ['a', 'b', 'c']
如何获取 list 中包含某个元素所在的下标
>>> ["foo","bar","baz"].index('bar')
1
如何扁平一个二维数组
>>> import itertools
>>> list2d = [[1,2,3],[4,5,6], [7], [8,9]]
>>> merged = list(itertools.chain(*list2d))
# python >= 2.6
>>> import itertools
>>> list2d = [[1,2,3],[4,5,6], [7], [8,9]]
>>> merged = list(itertools.chain.from_iterable(list2d))
如何将一个列表切分成若干个长度相同的子序列
[l[i:i+n] for i in range(0, len(l), n)]
按照元素对list进行split
[list(group) for k, group in groupby(
splitted_list, lambda x: x == "") if not k]
# ['a', '', '', 'a', 'a'] --> [['a'], ['a', 'a']]
Collections
Counter
Counter.most_common(n: int):List the n most common elements and their counts from the most common to the least. If n is None, then list all element counts.
>>> Counter('abcdeabcdabcaba').most_common(3)
[('a', 5), ('b', 4), ('c', 3)]
Itertools
itertools.groupby【把相邻的压缩】
import itertools
s = "aabbssddeeessddaassbbdds"
res = [(digit, list(group))for digit, group in itertools.groupby(s)]
for x in res:
print(x)
# ('a', ['a', 'a'])
# ('b', ['b', 'b'])
# ('s', ['s', 's'])
# ('d', ['d', 'd'])
# ('e', ['e', 'e', 'e'])
# ('s', ['s', 's'])
# ('d', ['d', 'd'])
# ('a', ['a', 'a'])
# ('s', ['s', 's'])
# ('b', ['b', 'b'])
# ('d', ['d', 'd'])
# ('s', ['s'])
itertools.permutations and combinations
from itertools import permutations, combinations
permutations('ABCD', 2) # --> AB AC AD BA BC BD CA CB CD DA DB DC
combinations('ABCD', 2) # --> AB AC AD BC BD CD
itertools.product
import itertools
a = [''.join(x) for x in itertools.product('01', repeat=3)]
print(a)
# ['000', '001', '010', '011', '100', '101', '110', '111']
Setisdisjoint(other)【重要!】:Return True if the set has no elements in common with other. Sets are disjoint if and only if their intersection is the empty set.
set <= other:Test whether every element in the set is in other.
set < other:Test whether the set is a proper subset of other, that is, set <= other and set != other.
set >= other:Test whether every element in other is in the set.
set > other:Test whether the set is a proper superset of other, that is, set >= other and set != other.
union(*others): set | other | ...
intersection(*others):set & other & ...
difference(*others):set - other - ...
symmetric_difference(other):set ^ other
copy():Return a shallow copy of the set.
set 必须传入可迭代对象 set([x])【注意!】
多Set批量操作
lis = [set((0,1,3)), set((1,3))]
set.intersection(*lis)
# set([1, 3])
set.union(*lis)
# set([0, 1, 3])
Sort用lst.sort() 而不是nlst = sorted(lst):区别在于lst.sort()是 in-place sort,改变lst, sorted会创建新list,成本比较高。
按照类的成员排序
cluster_pool = sorted(cluster_pool, key=operator.attrgetter('count'), reverse=True)
字典按照键值排序
belongs_list = sorted(meta.title_belongs.items(), key=operator.itemgetter(1), reverse=True)
python comparetor
def cmp_to_key(mycmp):
'Convert a cmp= function into a key= function'
class K:
def __init__(self, obj, *args):
self.obj = obj
def __lt__(self, other):
return mycmp(self.obj, other.obj) < 0
def __gt__(self, other):
return mycmp(self.obj, other.obj) > 0
def __eq__(self, other):
return mycmp(self.obj, other.obj) == 0
def __le__(self, other):
return mycmp(self.obj, other.obj) <= 0
def __ge__(self, other):
return mycmp(self.obj, other.obj) >= 0
def __ne__(self, other):
return mycmp(self.obj, other.obj) != 0
return K
To convert to a key function, just wrap the old comparison function:
>>> sorted([5, 2, 4, 1, 3], key=cmp_to_key(reverse_numeric))
[5, 4, 3, 2, 1]
Other
列表解析 和 map 的性能函数:map 性能好一点
$ python -mtimeit -s'xs=range(10)' 'map(hex, xs)'
100000 loops, best of 3: 4.86 usec per loop
$ python -mtimeit -s'xs=range(10)' '[hex(x) for x in xs]'
100000 loops, best of 3: 5.58 usec per looplambda:列表解析好一点
$ python -mtimeit -s'xs=range(10)' 'map(lambda x: x+2, xs)'
100000 loops, best of 3: 4.24 usec per loop
$ python -mtimeit -s'xs=range(10)' '[x+2 for x in xs]'
100000 loops, best of 3: 2.32 usec per loop
Python 2.7 里[...]是什么?
What is this […]?
[1, [...], 2]
它意味着你在它的内部创建了一个无限嵌套的不能打印的列表。p包含一个包含p的p...等等。[...]就是一种让你知道问题的标记
动态条件
while any(os.path.exists(prefix + extension) for extension in extensions):
链式调用
# 返回self就好了
# 顺序是先1后2
# 结果:[1, 2]
class chain():
def __init__(self):
self.s = []
def append(self, i):
self.s.append(i)
# 返回self
return self
def __str__(self):
return str(self.s)
item = chain()
item.append(1).append(2)
print(item)