On Dec 13, 6:11 pm, [EMAIL PROTECTED] wrote: > from random import randrange > from itertools import imap, repeat > from operator import getitem, add, getslice > > result = 0 > zeros = [0]*100 > for i in xrange (100000): > s = [chr(randrange(128))] * 1024 > starts = repeat(randrange(900), 100) > ends = imap(add, starts, repeat(randrange(1,100), 100)) > substrs = imap(getslice, repeat(s, 100), starts, ends) > result += sum(imap(ord, imap(getitem, substrs, zeros))) > > print "Sum is ", result > > There's got to be a simpler and more efficient way to do this. > Can you help? > > Thanks, > igor
repeat(randrange(n), p) doesn't do what you want it to: It generates a single random number less thant n and repeats it p times >>> list(repeat(randrange(100), 10)) [54, 54, 54, 54, 54, 54, 54, 54, 54, 54] Instead you want imap(randrange, repeat(n, p)): >>> map(randrange, repeat(100, 10)) [69, 68, 81, 26, 60, 76, 40, 55, 76, 75] I don't understand why you make the Python version so complicated. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list