[Tutor] Location of found item in list.

Luke Paireepinart rabidpoobear at gmail.com
Thu Oct 19 08:41:45 CEST 2006


Chris Hengge wrote:
> If that last post to Luke didn't clear anything up.. lets try this at 
> a smaller level...
> I'm going to ignore the fact that my program already renames the file 
> properly..
>
> I have list1 of filenames
> I have list2 of filenames
>
> Item from list 1 needs to be found in list2, but I can't change cases, 
> because thats what I'm looking for. I suppose, I could .sort each list 
> after chopping the extra files names out of list2 that aren't in 
> list1. then all I have to do is list1[count] = list2[count]
>
> What I'd like to know, is why my program works, if I can't get it to 
> display the proper filename in the 3rd output column. I guess 
> os.rename(notcasesensitive, iscasesensitive) ?
>
I doubt os.rename is case insensitive for the first argument.

Okay, here is what you're trying to do.
Assuming I haven't seen your code before and I'm writing it from scratch.

I have a list of filenames.
list1 = ['a.exe', 'b.exe', 'c.exe', 'e.exe']
I could've read these in from a file, or anything.
It doesn't matter where they came from.
The important thing is that they have the case that I want.

Say I also have another list.
list2 = ['a.ExE',' B.exe', 'C.EXE', 'd.EXE','E.eXe']
we got this list maybe from a directory listing or something.  It 
doesn't matter.

What we're trying to do is: apply any case changes that are necessary to 
make all of list1's items' counterparts
in list2 be the same.  I.E., 'a.ExE' in list2 becomes 'a.exe' from list1.

We'll assume that if there is an item in list2 that doesn't have a 
counterpart in list1,
that we just don't care.  So 'd.EXE' is going to be 'd.EXE' after the 
program is run,
because there is no corresponding item in list1.  In other words,
if there are extra files in the directory that we don't need to fix the 
case for, we'll just ignore them.


So, without thinking about the implementation, what is it we want to do?

for every item in list1,
check it against each item in list2.
if the case-insensitive version of the list1 item is equal to the 
case-insensitive version of list2,
and the case-sensitive versions aren't equal,
we know we need to change the case.

Otherwise, if the case-sensitive versions are equal,
we don't need to change case, but we'll tell them so.

if the case-insensitive version of list1 item is not equal to the 
case-insensitive version of the list2 item,
we don't care.  It's one of the things that we don't need to change.


So what would this look like in python code?

for item1 in list1:
    for item2 in list2:
       if item1.lower() == item2.lower(): #they're the same file if you 
ignore case
          if item1 != item2: #but there's some case error,
             os.rename(item1,item2)
             print "%s is not %s" % (item1, item2)
          else:
             print "%s is %s" % (item1,item1)# or (item1, item1), or 
(item2,item2), since they're equivalent.

I think this is what you wanted.
The way you were trying to explain it just confused everyone.

Hope that helps,
-Luke



More information about the Tutor mailing list