Sequence traversal

Sven Havemann s.havemann at tu-bs.de
Thu Sep 23 11:44:21 EDT 1999


Hi!
===

List traversal is elegant in python! - But what if I want to step
through two sequences in parallel? 

l1 = list("Names ")
l2 = [4,2,1,5,3,9]

for i in l1:                    #   cool and
  if i=='a': print "a found"    #   very elegant!

for i in range(len(l1)):        #   not so cool
  if l1[i]=='a' and l2[i]==2: print "a and 2 found!"

I assume that indexing in a list l is not too efficient (O(log
len(l))?), while stepping through l using 'for i in l' will certainly be
O(1). - Before I look through the python sources or make performance
measures I just wanted to know whether there's a more elegant solution
to this!!!

TIA for any hints,
Greetings, Sven.

PS. 
Please write back directly to me, I don't have easy news access.

-- 
__________________________________________________________________
 dipl-inform. Sven Havemann        Institut fuer ComputerGraphik
 Odastrasse 6                      Rebenring 18
 38122 Braunschweig - Germany      38106 Braunschweig - Germany
 Tel. 0531/2808955                 Tel. 0531/391-2108, Fax: -2103
 mailto:s.havemann at tu-bs.de        http://www.cg.cs.tu-bs.de




More information about the Python-list mailing list