##// END OF EJS Templates
don't create lists...
MinRK -
Show More
@@ -56,9 +56,9 else:
56 56 arrayModules.append({'module':numarray,
57 57 'type':numarray.numarraycore.NumArray})
58 58
59 class Map:
59 class Map(object):
60 60 """A class for partitioning a sequence using a map."""
61
61
62 62 def getPartition(self, seq, p, q):
63 63 """Returns the pth partition of q partitions of seq."""
64 64
@@ -66,25 +66,24 class Map:
66 66 if p<0 or p>=q:
67 67 print "No partition exists."
68 68 return
69
70 remainder = len(seq)%q
71 basesize = len(seq)//q
72 hi = []
73 lo = []
74 for n in range(q):
75 if n < remainder:
76 lo.append(n * (basesize + 1))
77 hi.append(lo[-1] + basesize + 1)
78 else:
79 lo.append(n*basesize + remainder)
80 hi.append(lo[-1] + basesize)
69
70 N = len(seq)
71 remainder = N % q
72 basesize = N // q
73
74 if p < remainder:
75 low = p * (basesize + 1)
76 high = low + basesize + 1
77 else:
78 low = p * basesize + remainder
79 high = low + basesize
81 80
82 81 try:
83 result = seq[lo[p]:hi[p]]
82 result = seq[low:high]
84 83 except TypeError:
85 84 # some objects (iterators) can't be sliced,
86 85 # use islice:
87 result = list(islice(seq, lo[p], hi[p]))
86 result = list(islice(seq, low, high))
88 87
89 88 return result
90 89
General Comments 0
You need to be logged in to leave comments. Login now