Twitter中Follower和Followee,现需要找到互相关注的两个人(不关心顺序)
例如:现有列表
l = [(1, 2), (2, 3), (3, 2), (3, 4), (4, 1), (4, 3), (4, 3)]
可以通过下列函数生成
def gen_pairs(): return (random.randint(0, 30), random.randint(0, 30)) l = [gen_pairs() for x in xrange(20)]
解法一:
import collections [x for x, y in collections.Counter([tuple(sorted(x)) for x in l]).iteritems() if y > 1]
- [tuple(sorted(x)) for x in l] 首先是将列表的内容按小到大重新排列
- 通过计数器collections.Counter,来统计重复的数量
- if y > 1 将大于一个的放入结果集中
最后统计用时best of 3: 38.9 ?s per loop
老湿,还能给力点吗?
解法二:
[x for x in set_l if x[::-1] in set(l)]
快了6倍……答主说到这个算法最快也就是O(n)了,因为必须遍历所有项有木有啊
第47节:辗转相除法
或称欧几里得求最大公约数算法
#!/usr/bin/env python # encoding: utf-8 def Euclid(x, y): if x < y: tmp = x x = y y = tmp while y != 0: rod = x % y x = y y = rod return x if __name__ == '__main__': print Euclid(126, 90) # should be 18
第50节:桶排序
#!/usr/bin/env python # encoding: utf-8 def bucket_sort(lis): max_item = max(lis) bucket = [-1 for x in xrange(max_item+1)] for item in lis: if item < 0: raise ValueError("Can't handle %d" % item) bucket[int(item)] = item return filter(lambda x: x != -1, bucket) if __name__ == '__main__': print bucket_sort([8,2,1,5,9,7]) # should be [1,2,5,7,8,9]
第51节:基位排序——桶排序的升级版
#!/usr/bin/env python # encoding: utf-8 from copy import deepcopy def bit_sort(lis): bucket = [[] for x in xrange(len(lis))] k = len(str(max(lis))) # sort time array = [deepcopy(bucket) for x in xrange(k)] for time in xrange(k): if time == 0: for l in lis: array[0][int(str(l)[-1])].append(l) else: for b in array[time-1]: for item in b: try: array[time][int(str(item)[-1-time])].append(item) except IndexError: array[time][0].append(item) return array if __name__ == '__main__': for line in bit_sort([123, 602, 82, 777, 57, 510, 396, 196, 843, 138]): print line print "The Last line should be:[[57, 82], [123, 138, 196], [], [396], [], [510], [602], [777], [843], []]"