import random import array import numpy from timeit import Timer numlines = 500 numcols = 120 #build numlines line, numcols column list on which to operate. #Could do this in one line with nested comprehensions random.seed() a = [] for i in range(0,numlines): a.append([x * random.randint(0,500) for x in range(1,numcols)]) #Now build list of arrays. #Can't use list comprehensions because lines are arrays too #Can't use 2d array because arrays can only hold base types? b = [] for i in range(0,numlines): l = array.array('i') for x in range(1,numcols): l.append(x * random.randint(0,500)) b.append(l) #easier with numeric c = numpy.ones( (numlines,numcols) ) for x,l in enumerate(c): c[x] = map(lambda line: random.randint(0,500),l) def not70(x): if x == 70: return 0 else: return x def replaceList(a): """loop through 2d list with for loop, using map to find and replace values in lines of array""" for index, line in enumerate(a): a[index] = map(not70, line) def letsUseALittleMath(a): b = numpy.not_equal(a, 70) a = a * b #quicker way? def fastway(a): a[a==70]=0 if __name__=='__main__': from timeit import Timer reps = 500 t = Timer("replaceList(a)", "from __main__ import replaceList, a") print "Time using all lists: %s " % t.timeit(reps) t = Timer("replaceList(b)", "from __main__ import replaceList, b") print "Time using lists of arrays: %s" % t.timeit(reps) t = Timer("letsUseALittleMath(c)", "from __main__ import letsUseALittleMath, c") print "Time using Numpy arrays and array multiplication: %s" % t.timeit(reps) #t = Timer("replaceList(c)", "from __main__ import replaceList, c") #print "Time using Numeric arrays: %s" % t.timeit(reps) t = Timer("fastway(c)", "from __main__ import fastway, c") print "Time using fastway: %s" % t.timeit(reps)