[Tutor] Finding the differences between two lists
Robert Sjoblom
robert.sjoblom at gmail.com
Thu Aug 25 23:21:12 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?
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.
More information about the Tutor
mailing list