LISTS: Extract every other element

Mike Fletcher mfletch at tpresence.com
Fri Dec 17 06:16:39 EST 1999


With the little module below, I attempted to test the speed of your
implementation.  It is really quite good (not to mention linear with the
size of the data being processed). The Numeric approach requires about 1/2
the time in all cases. My (slightly modified) approach is slightly faster.
On data sets of 10**7 integers, time on my machine is 1.097 for my
list-based approach, and 0.451 for the Numeric approach, and 1.185 for your
implementation. 10**8 causes severe memory effects on my machine.

So, in conclusion, if you can use Numeric arrays, you can likely cut your
time by two (not counting whatever savings can be seen by not calling
Numeric.array(data) in the taking function).  If you cannot, I can't see a
more efficient approach using straight lists. All-in-all, none of these is a
really slow algorithm unless you're doing this many times.

Incidentally, the only difference between our algorithms is that I'm using
an integer multiply instead of an integer divide.

Hope this helps,
Mike

def test1( data, step=2 ):
	result = [None]*(len(data)/step)
	for index in xrange(len(data)/step):
		result[index]=data[index*step]
	return result

def test2( data, step=2, offset=0):
	import Numeric
	data = Numeric.array( data, Numeric.Int32 )
	print len(data)
	data.shape= (step, -1)
	return data[offset]

def test3( data, step=2):
	lst2 = [ None ] * (len(data)/2)
	for i in xrange( 0, len(data), 2 ):
		lst2[i/2] = data[i]



##print test1( testdata )
import profile
def bigTest( ):
	for exponent in range(1, 7): # up to 10**7 (10 million)
		global data
		data = range( 10**exponent)
		print "list"
		profile.run ("test1( data)")
		print "matrix"
		profile.run ("test2( data)")
		print "given"
		profile.run ("test3( data)")
if __name__ == '__main__':
	bigTest()

-----Original Message-----
From: Randall Hopper [mailto:aa8vb at yahoo.com]
Sent: Thursday, December 16, 1999 1:14 PM
To: python-list at python.org
Subject: LISTS: Extract every other element


I want to take a large list:

  [ 1,2,3,4,5,6,7,... ]

and build a list with every other element:

  [ 1,3,5,7,... ]


Is there a faster way than looping over indices?:

      lst2 = [ None ] * len(lst)/2
      for i in xrange( 0, len(lst), 2 ):
        lst2[i/2] = lst[i]

-- 
Randall Hopper
aa8vb at yahoo.com

-- 
http://www.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list