[Tutor] Location of found item in list.
rabidpoobear at gmail.com
Thu Oct 19 03:34:52 CEST 2006
Chris Hengge wrote:
> Ok the example I gave here wasn't written as I had it in my head.. you
> are right, the example only had 1 sentence.
> List1 ['a.exe','b.exe',c.exe']
> List2 ['A.exe',B.eXe',c.EXE']
> for item in List1:
> if item in List2:
> print item + " " + list2thing that matched.
> I can't force to upper or lower, that would break the already working
> Its just this darn output for list2 that isn't working.
> I tried the suggested list.index(item) but that wont work if there
> isn't a match.
Well, yeah, but it won't go into the if statement if there isn't a
match so what does that matter?
> Right now my code works when there is a match, and if there isnt'...
> It also works for renaming the actual file to match the file call from
> the document.
> I'm about to take the display out, since I dont honestly care that it
> works, but now that I've been working with it I'm being stubborn and
> want the darn thing to show me =P
Did you try my example yet?
It should work just fine.
just iterate over both of the lists...
for item1 in List1:
for item2 in List2:
if item1 == item2: #this is equivalent to 'if item1 in List2'
except will match ALL occurrences of item1 in List2. if you want to
only match the first, add a break.
print item1+ ' ' + item2
More information about the Tutor