[Tutor] Finding the differences between two lists

Justin Wendl justin.wendl at gmail.com
Fri Aug 26 00:40:58 CEST 2011


The lists were created using MS Notepad, and I forgot about the newlines -
so you were absolutely right!  So, is it possible to use strip() immediately
when reading a file into a list to avoid confusion down the road, and is
this common?

Thank you everyone who contributed to this thread :-)

- Justin

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

> > 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?
> Definitely not, it doesn't matter if they're sorted no matter what way
> you solve it. What matters is how the logfile looks to Python. Take
> this fake list I've made consisting of your two MAC addresses:
> scanResults = ['00:1C:14:BA:D9:E9\n', '00:16:3E:EB:04:D9\n']
> verifiedList = ['00:1C:14:BA:D9:E9\n', '00:16:3E:EB:04:D9']
> >>> scanResults == verifiedList
> False
> The difference, obviously, is that 00:16:3E:EB:04:D9 contains a
> newline \n in one of the files but not the other. As such, they're not
> identical. We can solve this (probably) by adding some more list
> comprehension when we open the files initially:
> with open("scanResults.txt", "r") as f:
>     scanResults = [line.strip() for line in f.readlines()]
> with open("verifiedList.txt", "r") as f:
>     verifiedList = [line.strip() for line in f.readlines()]
> badMacs = [item for item in scanResults if item not in verifiedList]
> I suspect that it's the newlines that are messing with you, but if
> it's something else I can't tell without the actual files.
> --
> 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/b818ba56/attachment.html>

More information about the Tutor mailing list