''' name = 'histo_tuple' ranking = ('iter_groupby','dict1') tests = ( Test([1,2,3,4,1,2,3,1,1,1,9]) == [(1,5),(2,2),(3,2),(4,1),(9,1)], Test([1,1,1,1,1,1,1,1,1,1,1]) == [(1,11)], ) @staticmethod def iter_groupby(arg): arg = as_any_array(arg) return sorted([(k,len(list(g))) for k,g in groupby(sorted(arg))]) @staticmethod def dict1(arg,out=None): arg = as_any_array(arg) out = {} for val in arg: out[val] = out.get(val,0) + 1 return sorted(out.items()) ######################################################################## if __name__ == "__main__": from base import tester tester.testmod() ######################################################################## # vim:sw=2:sts=2:expandtab:shiftround
Test([2.0]) == [], Test([1,3,5]) == [2.0,4.0], Test([1,3,3]) == [2.0], Test([1.2,1.3,1.5]) == [1.25,1.4], ) @staticmethod def naive(arg): uniq = sorted(set(arg)) out = [] for i in range(len(uniq)-1): a,b = uniq[i:i+2] out.append((a+b)/2.0) return out @staticmethod def vector(arg): uniq = as_num_array(sorted(set(arg))) if len(uniq) <= 1: return [] return (uniq[1:]+uniq[:-1])/2.0 ######################################################################## if __name__ == "__main__": from base import tester tester.testmod() ########################################################################