list index()

mensanator at aol.com mensanator at aol.com
Thu Aug 30 23:22:37 EDT 2007


On Aug 30, 9:26 pm, al... at mac.com (Alex Martelli) wrote:
> <zzbba... at aol.com> wrote:
>
>    ...
>
> > In my case of have done os.listdir() on two directories. I want to see
> > what files are in directory A that are not in directory B.
>
> So why would you care about WHERE, in the listdir of B, are to be found
> the files that are in A but not B?!  You should call .index only if you
> CARE about the position.
>
>
> is the "one obvious way to do it" (the set(...) is just a simple and
> powerful optimization -- checking membership in a set is roughly O(1),
> while checking membership in a list of N items is O(N)...).

Why wouldn't "the one obvious way" be:

 def inAnotB(A, B):
     inA  = set(os.listdir(A))
     inBs = set(os.listdir(B))
     return inA.difference(inBs)
?


>
> Alex





More information about the Python-list mailing list