[Tutor] Finding the differences between two lists

Justin Wendl justin.wendl at gmail.com
Thu Aug 25 22:22:27 CEST 2011


shantanoo, Andre, and Robert:

All of your solutions seem to work (and thank you for the tips!), however,
with each solution there seems to be 2 MACs that should not be in the
results.

00:1C:14:BA:D9:E9 and
00:16:3E:EB:04:D9

should not be be turning up in the results because they are in the
'verified' list.  These lists are not sorted - do you think that could be
the issue?

- Justin


On Thu, Aug 25, 2011 at 3:48 PM, Robert Sjoblom <robert.sjoblom at gmail.com>wrote:

> > Scenario: I have a list of MAC addresses that are known and good, and am
> > comparing it to a list of MACs found in a scan.  I want to weed out the
> > those which are unknown.  I am using IDLE (Python 2.7) on Windows, and
> all
> > files are in the same directory.
> >
> > Code:
> >
> > scanResults = open('scanResults.txt', 'r')
> > verifiedList = open('verifiedList.txt', 'r')
>
> You don't close the files after you open them, which is bad practice.
> "with" is a great keyword, because it closes the file once it's run
> its block of code. We'll use that:
>
> with open("scanResults.txt", "r") as f:      #open scanResults.txt and
> put the file object in variable f
>    scanResults = f.readlines()                  #once this finishes
> scanResults.txt is closed
> with open("verifiedList.txt", "r") as f:
>    verifiedList = f.readlines()
>
> > badMacs = []
> > for mac in scanResults:
> >  if mac not in verifiedList:
> >    print mac
> >    badMacs.append(mac)
> >  else:
> >    break
>
> I can't say for sure, since I don't know how your logs look, but this
> loop will break at the first match (I think!). You could try changing
> the break statement to a continue statement (or just ignoring it
> altogether -- if it's in verifiedList you don't need to do anything).
> Anyway, list comprehensions are something I've been experimenting with
> lately, because I need the practice, so I'm going to suggest that
> instead:
> badMacs = [item for item in scanResults if item not in verifiedList]
>
> Other than that, there's not much to say; the standard is 4 spaces
> indentation, personally I feel that 1 space is too little, since it's
> hard to see where one block ends and another begins.
>
> best regards,
> Robert S.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110825/35eac979/attachment-0001.html>


More information about the Tutor mailing list