def unique(s): """Return a list of the elements in s, but without duplicates. For example, unique([1,2,3,1,2,3]) is some permutation of [1,2,3], unique("abcabc") some permutation of ["a", "b", "c"], and unique(([1, 2], [2, 3], [1, 2])) some permutation of [[2, 3], [1, 2]]. For best speed, all sequence elements should be hashable. Then unique() will usually work in linear time. If not possible, the sequence elements should enjoy a total ordering, and if list(s).sort() doesn't raise TypeError it's assumed that they do enjoy a total ordering. Then unique() will usually work in O(N*log2(N)) time. If that's not possible either, the sequence elements must support equality-testing. Then unique() will usually work in quadratic time. """ n = len(s) if n == 0: return [] # Try using a dict first, as that's the fastest and will usually # work. If it doesn't work, it will usually fail quickly, so it # usually doesn't cost much to *try* it. It requires that all the # sequence elements be hashable, and support equality comparison. u = {} try: for x in s: u[x] = 1 except TypeError: del u # move on to the next method else: return u.keys() # We can't hash all the elements. Second fastest is to sort, # which brings the equal elements together; then duplicates are # easy to weed out in a single pass. # NOTE: Python's list.sort() was designed to be efficient in the # presence of many duplicate elements. This isn't true of all # sort functions in all languages or libraries, so this approach # is more effective in Python than it may be elsewhere. try: t = list(s) t.sort() except TypeError: del t # move on to the next method else: assert n > 0 last = t[0] lasti = i = 1 while i < n: if t[i] != last: t[lasti] = last = t[i] lasti += 1 i += 1 return t[:lasti] # Brute force is all that's left. u = [] for x in s: if x not in u: u.append(x) return u """ sage: p = 5 sage: F = GF(p) sage: E. = GF(p^2,"a") sage: G = GL(2,p) sage: M = MatrixSpace(E,2,2) sage: M1 = MatrixSpace(F,2,2) sage: V = VectorSpace(E,2) sage: X = ProjectiveSpace(1,E) sage: P = PGL(2,p) sage: v = V([1,a+1]); v; g; X(list(M(g)*v)) (1, a + 1) [2 1] [0 2] (a + 1 : 1) sage: time orbit_v = [X(list(M(g)*v)) for g in M1 if g.det()!=F(0)] CPU times: user 1.47 s, sys: 0.00 s, total: 1.47 s Wall time: 1.47 s sage: attach "/home/wdj/sagefiles/unique.sage" sage: uorbit_v = unique(orbit_v) sage: len(uorbit_v) 20 sage: v = V([1,1]); v; g; X(list(M(g)*v)) (1, 1) [4 4] [4 4] (1 : 1) sage: time orbit_v2 = [X(list(M(g)*v)) for g in M1 if g.det()!=F(0)] CPU times: user 1.45 s, sys: 0.00 s, total: 1.45 s Wall time: 1.46 s sage: uorbit_v2 = unique(orbit_v2) sage: len(uorbit_v2) 6 sage: G.order() 480 sage: R. = PolynomialRing(E,"x") sage: f = x^p-x sage: C = HyperellipticCurve(f) sage: pts = C.rational_points() sage: ptsF = [pt for pt in pts if not("a" in str(pt[0]) or "a" in str(pt[1]) or "a" in str(pt[2])> sage: ptsE = [pt for pt in pts if not(pt in ptsF)] sage: len(pts); len(ptsF); len(ptsE) 92 8 84 sage: R2. = PolynomialRing(E,"x,y") sage: FracR2 = FractionField(R2) sage: bA0 = FracR2(1) sage: bB1_0 = FracR2(y/(x^p-x)) sage: bB1_1 = FracR2(y*x/(x^p-x)) sage: bB1_2 = FracR2(y*x^2/(x^p-x)) sage: bB1_3 = FracR2(y*x^3/(x^p-x)) sage: bB1_4 = FracR2(y*x^4/(x^p-x)) sage: r1 = [bA0(pt[0],pt[1]) for pt in ptsE] sage: r2 = [bB1_0(pt[0],pt[1]) for pt in ptsE] sage: r3 = [bB1_1(pt[0],pt[1]) for pt in ptsE] sage: r4 = [bB1_2(pt[0],pt[1]) for pt in ptsE] sage: r5 = [bB1_3(pt[0],pt[1]) for pt in ptsE] sage: r6 = [bB1_4(pt[0],pt[1]) for pt in ptsE] sage: MS = MatrixSpace(E,6,len(ptsE)) sage: Ggenmat = MS([r1,r1,r3,r4,r5,r6]) sage: Cagcode = LinearCode(Ggenmat) sage: Cagcode Linear code of length 84, dimension 5 over Finite Field in a of size 7^2 sage: time Cagcode.minimum_distance() CPU times: user 0.32 s, sys: 0.01 s, total: 0.33 s Wall time: 238.86 s 77 """