pair lst = zip lst ( tail lst ) sorted lst predict = and [ predict x y | (x,y) <- pair lst]
def IsListSorted_guess(lst): listLen = len(lst) if listLen <= 1: return True #由首个元素和末尾元素猜测可能的排序规则 if lst[0] == lst[-1]: #列表元素相同 for elem in lst: if elem != lst[0]: return False elif lst[0] < lst[-1]: #列表元素升序 for i, elem in enumerate(lst[1:]): if elem < lst[i]: return False else: #列表元素降序 for i, elem in enumerate(lst[1:]): if elem > lst[i]: return False return True
def IsListSorted_sorted(lst): return sorted(lst) == lst or sorted(lst, reverse=True) == lst
def IsListSorted_forloop(lst, key=lambda x, y: x <= y): for i, elem in enumerate(lst[1:]): #注意,enumerate默认迭代下标从0开始 if not key(lst[i], elem): #if elem > lst[i]更快,但通用性差 return False return True
def IsListSorted_allenumk(lst, key=lambda x, y: x <= y): return all(key(lst[i], elem) for i, elem in enumerate(lst[1:])) import operator def IsListSorted_allenumo(lst, oCmp=operator.le): return all(oCmp(lst[i], elem) for i, elem in enumerate(lst[1:])) def IsListSorted_allenumd(lst): return all((lst[i] <= elem) for i, elem in enumerate(lst[1:])) def IsListSorted_allxran(lst, key=lambda x,y: x <= y): return all(key(lst[i],lst[i+1]) for i in xrange(len(lst)-1)) def IsListSorted_allzip(lst, key=lambda x,y: x <= y): from itertools import izip #Python 3中zip返回生成器(generator),而izip被废弃 return all(key(a, b) for (a, b) in izip(lst[:-1],lst[1:]))
def IsListSorted_numpy(arr, key=lambda dif: dif >= 0): import numpy try: if arr.dtype.kind == 'u': #无符号整数数组执行np.diff时存在underflow风险 arr = numpy.int64(lst) except AttributeError: pass #无dtype属性,非数组 return (key(numpy.diff(arr))).all() #numpy.diff(x)返回相邻数组元素的差值构成的数组
def IsListSorted_reduce(iterable, key=lambda x, y: x <= y):
cmpFunc = lambda x, y: y if key(x, y) else float('inf')
return reduce(cmpFunc, iterable, .0) < float('inf')
def IsListSorted_itermap(iterable, key=lambda x, y: x <= y): from itertools import imap, tee a, b = tee(iterable) #为单个iterable创建两个独立的iterator next(b, None) return all(imap(key, a, b))
def IsListSorted_iterzip(iterable, key=lambda x, y: x <= y): from itertools import tee, izip a, b = tee(iterable) next(b, None) return all(key(x, y) for x, y in izip(a, b)) def pairwise(iterable): from itertools import tee, izip a, b = tee(iterable) next(b, None) return izip(a, b) #"s -> (s0,s1), (s1,s2), (s2, s3), ..." def IsListSorted_iterzipf(iterable, key=lambda x, y: x <= y): return all(key(a, b) for a, b in pairwise(iterable))
def IsListSorted_fastd(lst): it = iter(lst) try: prev = it.next() except StopIteration: return True for cur in it: if prev > cur: return False prev = cur return True def IsListSorted_fastk(lst, key=lambda x, y: x <= y): it = iter(lst) try: prev = it.next() except StopIteration: return True for cur in it: if not key(prev, cur): return False prev = cur return True
import random def IsListSorted_rand(lst, randNum=3, randLen=100): listLen = len(lst) if listLen <= 1: return True #由首个元素和末尾元素猜测可能的排序规则 if lst[0] < lst[-1]: #列表元素升序 key = lambda dif: dif >= 0 else: #列表元素降序或相等 key = lambda dif: dif <= 0 threshold, sortedFlag = 10000, True import numpy if listLen <= threshold or listLen <= randLen*2 or not randNum: return (key(numpy.diff(numpy.array(lst)))).all() from random import sample for i in range(randNum): sortedRandList = sorted(sample(xrange(listLen), randLen)) flag = (key(numpy.diff(numpy.array([lst[x] for x in sortedRandList])))).all() sortedFlag = sortedFlag and flag return sortedFlag
>>>import random >>> random.sample(range(10), 10); random.sample(range(10), 5) [9, 1, 6, 3, 0, 8, 4, 2, 7, 5] [1, 2, 5, 6, 0] >>> rand = [random.randint(1,10) for i in range(10)]; rand [7, 3, 7, 5, 7, 2, 4, 4, 9, 8] >>> random.sample(rand, 5); random.sample(rand, 5) [4, 7, 7, 9, 8] [3, 9, 2, 5, 7] >>> randGen = (random.randint(1,10) for i in range(10)); randGen <generator object <genexpr> at 0x0192C878>
from time import clock TimeList = [] def FuncTimer(repeats=1000): def decorator(func): def wrapper(*args, **kwargs): try: startTime = clock() for i in xrange(repeats): ret = func(*args, **kwargs) except Exception, e: print '%s() Error: %s!' %(func.__name__, e) ret = None finally: #当目标函数发生异常时,仍旧输出计时信息 endTime = clock() timeElasped = (endTime-startTime)*1000.0 msg = '%21s(): %s =>Time Elasped: %.3f msec, repeated %d time(s).' \ %(func.__name__, ret, timeElasped, repeats) global TimeList; TimeList.append([timeElasped, msg]) return ret return wrapper return decorator def ReportTimer(): global TimeList; TimeList.sort(key=lambda x:x[0]) for entry in TimeList: print entry[1] TimeList = [] #Flush
def TestHeadUnorderedList(): TEST_NAME = 'HeadUnorderedList'; scale = int(1e5) List = random.sample(xrange(scale), scale) + range(scale) print 'Test 1: %s, list len: %d' %(TEST_NAME, len(List)) IsListSorted_guess(List) IsListSorted_sorted(List) IsListSorted_allenumk(List) IsListSorted_allenumo(List) IsListSorted_allenumd(List) IsListSorted_allxran(List) IsListSorted_allzip(List) IsListSorted_forloop(List) IsListSorted_itermap(List) IsListSorted_iterzipf(List) IsListSorted_iterzip(List) IsListSorted_reduce(List) IsListSorted_numpy(numpy.array(List)) #若不先转换为数组,则耗时骤增 IsListSorted_fastd(List) IsListSorted_fastk(List) ReportTimer()
Test 1: HeadUnorderedList, list len: 200 IsListSorted_fastd(): False =>Time Elasped: 0.757 msec, repeated 1000 time(s). IsListSorted_fastk(): False =>Time Elasped: 1.091 msec, repeated 1000 time(s). IsListSorted_forloop(): False =>Time Elasped: 2.080 msec, repeated 1000 time(s). IsListSorted_guess(): False =>Time Elasped: 2.123 msec, repeated 1000 time(s). IsListSorted_allxran(): False =>Time Elasped: 2.255 msec, repeated 1000 time(s). IsListSorted_allenumd(): False =>Time Elasped: 2.672 msec, repeated 1000 time(s). IsListSorted_allenumo(): False =>Time Elasped: 3.021 msec, repeated 1000 time(s). IsListSorted_allenumk(): False =>Time Elasped: 3.207 msec, repeated 1000 time(s). IsListSorted_itermap(): False =>Time Elasped: 5.845 msec, repeated 1000 time(s). IsListSorted_allzip(): False =>Time Elasped: 7.793 msec, repeated 1000 time(s). IsListSorted_iterzip(): False =>Time Elasped: 9.667 msec, repeated 1000 time(s). IsListSorted_iterzipf(): False =>Time Elasped: 9.969 msec, repeated 1000 time(s). IsListSorted_numpy(): False =>Time Elasped: 16.203 msec, repeated 1000 time(s). IsListSorted_sorted(): False =>Time Elasped: 45.742 msec, repeated 1000 time(s). IsListSorted_reduce(): False =>Time Elasped: 145.447 msec, repeated 1000 time(s). Test 1: HeadUnorderedList, list len: 200000 IsListSorted_fastd(): False =>Time Elasped: 0.717 msec, repeated 1000 time(s). IsListSorted_fastk(): False =>Time Elasped: 0.876 msec, repeated 1000 time(s). IsListSorted_allxran(): False =>Time Elasped: 2.104 msec, repeated 1000 time(s). IsListSorted_itermap(): False =>Time Elasped: 6.062 msec, repeated 1000 time(s). IsListSorted_iterzip(): False =>Time Elasped: 7.244 msec, repeated 1000 time(s). IsListSorted_iterzipf(): False =>Time Elasped: 8.491 msec, repeated 1000 time(s). IsListSorted_numpy(): False =>Time Elasped: 801.916 msec, repeated 1000 time(s). IsListSorted_forloop(): False =>Time Elasped: 2924.755 msec, repeated 1000 time(s). IsListSorted_guess(): False =>Time Elasped: 2991.756 msec, repeated 1000 time(s). IsListSorted_allenumo(): False =>Time Elasped: 3025.864 msec, repeated 1000 time(s). IsListSorted_allenumk(): False =>Time Elasped: 3062.792 msec, repeated 1000 time(s). IsListSorted_allenumd(): False =>Time Elasped: 3190.896 msec, repeated 1000 time(s). IsListSorted_allzip(): False =>Time Elasped: 6586.183 msec, repeated 1000 time(s). IsListSorted_sorted(): False =>Time Elasped: 119974.955 msec, repeated 1000 time(s). IsListSorted_reduce(): False =>Time Elasped: 154747.659 msec, repeated 1000 time(s).
def TestMiddUnorderedList(): TEST_NAME = 'MiddUnorderedList'; scale = int(1e5) List = range(scale) + random.sample(xrange(scale), scale) + range(scale) print 'Test 2: %s, list len: %d' %(TEST_NAME, len(List)) IsListSorted_numpy(numpy.array(List)) # 1572.295 msec IsListSorted_guess(List) # 14540.637 msec IsListSorted_itermap(List) # 21013.096 msec IsListSorted_fastk(List) # 23840.582 msec IsListSorted_allxran(List) # 31014.215 msec IsListSorted_iterzip(List) # 33386.059 msec IsListSorted_forloop(List) # 34228.006 msec IsListSorted_allenumk(List) # 34416.802 msec IsListSorted_allzip(List) # 42370.528 msec IsListSorted_sorted(List) # 142592.756 msec IsListSorted_reduce(List) # 187514.967 msec ReportTimer()
def TestTailUnorderedList(): TEST_NAME = 'TailUnorderedList'; scale = int(1e5) List = range(scale, 0, -1) + random.sample(xrange(scale), scale) print 'Test 3: %s, list len: %d' %(TEST_NAME, len(List)) IsListSorted_numpy(numpy.array(List), key=lambda dif: dif <= 0) # 980.789 msec IsListSorted_guess(List) # 13273.862 msec IsListSorted_itermap(List, key=lambda x, y: x >= y) # 21603.315 msec IsListSorted_fastk(List, key=lambda x, y: x >= y) # 24183.548 msec IsListSorted_allxran(List, key=lambda x, y: x >= y) # 32850.254 msec IsListSorted_forloop(List, key=lambda x, y: x >= y) # 33918.848 msec IsListSorted_iterzip(List, key=lambda x, y: x >= y) # 34505.809 msec IsListSorted_allenumk(List, key=lambda x, y: x >= y) # 35631.625 msec IsListSorted_allzip(List, key=lambda x, y: x >= y) # 40076.330 msec ReportTimer()
def TestUnorderedList(): TEST_NAME = 'UnorderedList'; scale = int(1e5) List = random.sample(xrange(scale), scale) print 'Test 4: %s, list len: %d' %(TEST_NAME, len(List)) IsListSorted_fastk(List) # 0.856 msec IsListSorted_allxran(List) # 3.438 msec IsListSorted_iterzip(List) # 7.233 msec IsListSorted_itermap(List) # 7.595 msec IsListSorted_numpy(numpy.array(List)) # 207.222 msec IsListSorted_allenumk(List) # 953.423 msec IsListSorted_guess(List) # 1023.575 msec IsListSorted_forloop(List) # 1076.986 msec IsListSorted_allzip(List) # 2062.821 msec ReportTimer()
```python def TestSameElemList(): TEST_NAME = 'SameElemList'; scale = int(1e5) List = [5]*scale print 'Test 5: %s, list len: %d' %(TEST_NAME, len(List)) IsListSorted_numpy(numpy.array(List)) # 209.324 msec IsListSorted_sorted(List) # 2760.139 msec IsListSorted_guess(List) # 5843.942 msec IsListSorted_itermap(List) # 20609.704 msec IsListSorted_fastk(List) # 23035.760 msec IsListSorted_forloop(List) # 29043.206 msec IsListSorted_allenumk(List) # 29553.716 msec IsListSorted_allxran(List) # 30348.549 msec IsListSorted_iterzip(List) # 32806.217 msec ReportTimer()
def TestAscendingList(): TEST_NAME = 'AscendingList'; scale = int(1e5) List = range(scale) print 'Test 6: %s, list len: %d' %(TEST_NAME, len(List)) IsListSorted_numpy(numpy.array(List)) # 209.217 msec IsListSorted_sorted(List) # 2845.166 msec IsListSorted_fastd(List) # 5977.520 msec IsListSorted_guess(List) # 10408.204 msec IsListSorted_allenumd(List) # 15812.754 msec IsListSorted_itermap(List) # 21244.476 msec IsListSorted_fastk(List) # 23900.196 msec IsListSorted_allenumo(List) # 28607.724 msec IsListSorted_forloop(List) # 30075.538 msec IsListSorted_allenumk(List) # 30274.017 msec IsListSorted_allxran(List) # 31126.404 msec IsListSorted_reduce(List) # 32940.108 msec IsListSorted_iterzip(List) # 34188.312 msec IsListSorted_iterzipf(List) # 34425.097 msec IsListSorted_allzip(List) # 37967.447 msec ReportTimer()
def TestDescendingList(): TEST_NAME = 'DescendingList'; scale = int(1e2) List = range(scale, 0, -1) print 'Test 7: %s, list len: %d' %(TEST_NAME, len(List)) IsListSorted_numpy(numpy.array(List), key=lambda dif: dif <= 0) # 209.318 msec IsListSorted_sorted(List) # 5707.067 msec IsListSorted_guess(List) # 10549.928 msec IsListSorted_itermap(List, key=lambda x, y: x >= y) # 21529.547 msec IsListSorted_fastk(List, key=lambda x, y: x >= y) # 24264.465 msec import operator; IsListSorted_allenumo(List, oCmp=operator.ge) # 28093.035 msec IsListSorted_forloop(List, key=lambda x, y: x >= y) # 30745.943 msec IsListSorted_allenumk(List, key=lambda x, y: x >= y) # 32696.205 msec IsListSorted_allxran(List, key=lambda x, y: x >= y) # 33431.473 msec IsListSorted_allzip(List, key=lambda x, y: x >= y) # 34837.019 msec IsListSorted_iterzip(List, key=lambda x, y: x >= y) # 35237.475 msec IsListSorted_reduce(List, key=lambda x, y: x >= y) # 37035.270 msec ReportTimer()
def TestIter(): TEST_NAME = 'Iter'; scale = int(1e7) List = range(scale) #升序 # List = random.sample(xrange(scale), scale) #乱序 print 'Test 8: %s, list len: %d' %(TEST_NAME, len(List)) iterL = iter(List); IsListSorted_guess(list(iterL)) iterL = iter(List); IsListSorted_sorted(iterL) iterL = iter(List); IsListSorted_itermap(iterL) iterL = iter(List); IsListSorted_iterzipf(iterL) iterL = iter(List); IsListSorted_iterzip(iterL) iterL = iter(List); IsListSorted_reduce(iterL) iterL = iter(List); IsListSorted_fastd(iterL) iterL = iter(List); IsListSorted_fastk(iterL, key=lambda x, y: x >= y) ReportTimer()
Test 8: Iter, list len: 10000000 ---升序 IsListSorted_fastd(): True =>Time Elasped: 611.028 msec, repeated 1 time(s). IsListSorted_sorted(): False =>Time Elasped: 721.751 msec, repeated 1 time(s). IsListSorted_guess(): True =>Time Elasped: 1142.065 msec, repeated 1 time(s). IsListSorted_itermap(): True =>Time Elasped: 2097.696 msec, repeated 1 time(s). IsListSorted_fastk(): True =>Time Elasped: 2337.233 msec, repeated 1 time(s). IsListSorted_reduce(): True =>Time Elasped: 3307.361 msec, repeated 1 time(s). IsListSorted_iterzipf(): True =>Time Elasped: 3354.034 msec, repeated 1 time(s). IsListSorted_iterzip(): True =>Time Elasped: 3442.520 msec, repeated 1 time(s). Test 8: Iter, list len: 10000000 ---乱序 IsListSorted_fastk(): False =>Time Elasped: 0.004 msec, repeated 1 time(s). IsListSorted_fastd(): False =>Time Elasped: 0.010 msec, repeated 1 time(s). IsListSorted_iterzip(): False =>Time Elasped: 0.015 msec, repeated 1 time(s). IsListSorted_itermap(): False =>Time Elasped: 0.055 msec, repeated 1 time(s). IsListSorted_iterzipf(): False =>Time Elasped: 0.062 msec, repeated 1 time(s). IsListSorted_guess(): False =>Time Elasped: 736.810 msec, repeated 1 time(s). IsListSorted_reduce(): False =>Time Elasped: 8919.611 msec, repeated 1 time(s). IsListSorted_sorted(): False =>Time Elasped: 12273.018 msec, repeated 1 time(s).
def TestRandList():
scale = int(1e6)
List = random.sample(xrange(scale), scale) + range(scale)
print 'Test 1: %s, list len: %d' %('HeadUnorderedList', len(List))
IsListSorted_fastk(List)
IsListSorted_numpy(numpy.array(List))
IsListSorted_rand(List, randNum=1)
ReportTimer()
List = range(scale) + random.sample(xrange(scale), scale) + range(scale)
print 'Test 2: %s, list len: %d' %('MiddUnorderedList', len(List))
IsListSorted_fastk(List)
IsListSorted_numpy(numpy.array(List))
IsListSorted_rand(List, randNum=1)
ReportTimer()
List = range(scale, 0, -1) + random.sample(xrange(scale), scale)
print 'Test 3: %s, list len: %d' %('TailUnorderedList', len(List))
IsListSorted_fastk(List, key=lambda x, y: x >= y)
IsListSorted_numpy(numpy.array(List), key=lambda dif: dif <= 0)
IsListSorted_rand(List, randNum=1)
ReportTimer()
List = [random.randint(1,scale) for i in xrange(scale)] #random.sample(xrange(scale), scale)
print 'Test 4: %s, list len: %d' %('UnorderedList', len(List))
IsListSorted_fastk(List)
IsListSorted_numpy(numpy.array(List))
IsListSorted_rand(List, randNum=1)
ReportTimer()
List = [5]*scale
print 'Test 5: %s, list len: %d' %('SameElemList', len(List))
IsListSorted_fastk(List)
IsListSorted_numpy(numpy.array(List))
IsListSorted_rand(List, randNum=1)
ReportTimer()
List = range(scale)
print 'Test 6: %s, list len: %d' %('AscendingList', len(List))
IsListSorted_fastk(List)
IsListSorted_numpy(numpy.array(List))
IsListSorted_rand(List, randNum=1)
ReportTimer()
List = range(scale, 0, -1)
print 'Test 7: %s, list len: %d' %('DescendingList', len(List))
IsListSorted_fastk(List, key=lambda x, y: x >= y)
IsListSorted_numpy(numpy.array(List), key=lambda dif: dif <= 0)
IsListSorted_rand(List, randNum=1)
ReportTimer()
List = range(scale, 0, -1); List[scale/2]=0
print 'Test 8: %s, list len: %d' %('MiddleNotchList', len(List))
IsListSorted_fastk(List, key=lambda x, y: x >= y)
IsListSorted_numpy(numpy.array(List), key=lambda dif: dif <= 0)
IsListSorted_rand(List, randNum=1)
IsListSorted_rand(List, randNum=1, randLen=scale/2)
ReportTimer()
Test 1: HeadUnorderedList, list len: 2000000 IsListSorted_fastk(): False =>Time Elasped: 0.095 msec, repeated 1 time(s). IsListSorted_rand(): False =>Time Elasped: 0.347 msec, repeated 1 time(s). IsListSorted_numpy(): False =>Time Elasped: 7.893 msec, repeated 1 time(s). Test 2: MiddUnorderedList, list len: 3000000 IsListSorted_rand(): False =>Time Elasped: 0.427 msec, repeated 1 time(s). IsListSorted_numpy(): False =>Time Elasped: 11.868 msec, repeated 1 time(s). IsListSorted_fastk(): False =>Time Elasped: 210.842 msec, repeated 1 time(s). Test 3: TailUnorderedList, list len: 2000000 IsListSorted_rand(): False =>Time Elasped: 0.355 msec, repeated 1 time(s). IsListSorted_numpy(): False =>Time Elasped: 7.548 msec, repeated 1 time(s). IsListSorted_fastk(): False =>Time Elasped: 280.416 msec, repeated 1 time(s). Test 4: UnorderedList, list len: 1000000 IsListSorted_fastk(): False =>Time Elasped: 0.074 msec, repeated 1 time(s). IsListSorted_rand(): False =>Time Elasped: 0.388 msec, repeated 1 time(s). IsListSorted_numpy(): False =>Time Elasped: 3.757 msec, repeated 1 time(s). Test 5: SameElemList, list len: 1000000 IsListSorted_rand(): True =>Time Elasped: 0.304 msec, repeated 1 time(s). IsListSorted_numpy(): True =>Time Elasped: 3.955 msec, repeated 1 time(s). IsListSorted_fastk(): True =>Time Elasped: 210.977 msec, repeated 1 time(s). Test 6: AscendingList, list len: 1000000 IsListSorted_rand(): True =>Time Elasped: 0.299 msec, repeated 1 time(s). IsListSorted_numpy(): True =>Time Elasped: 4.822 msec, repeated 1 time(s). IsListSorted_fastk(): True =>Time Elasped: 214.171 msec, repeated 1 time(s). Test 7: DescendingList, list len: 1000000 IsListSorted_rand(): True =>Time Elasped: 0.336 msec, repeated 1 time(s). IsListSorted_numpy(): True =>Time Elasped: 3.867 msec, repeated 1 time(s). IsListSorted_fastk(): True =>Time Elasped: 279.322 msec, repeated 1 time(s). Test 8: MiddleNotchList, list len: 1000000 IsListSorted_rand(): True =>Time Elasped: 0.387 msec, repeated 1 time(s). IsListSorted_numpy(): False =>Time Elasped: 4.792 msec, repeated 1 time(s). IsListSorted_rand(): False =>Time Elasped: 78.903 msec, repeated 1 time(s). IsListSorted_fastk(): False =>Time Elasped: 110.444 msec, repeated 1 time(s).
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有