I’m reading from two files (lists of datetimes), called A and B. (not necessarily in order)
I’d like to create a list of these datetimes as objects:
class AB(object):
def __init__(self, datetime, a=False, b=False):
self.datetime = datetime
self.a = a
self.b = b
a should be set to true if the datetime exists in file A, and b true if datetime exists in file B. (false otherwise)
For example:
file A: 20111225, 20111226
file B: 20111225, 20111227
List of objects should be:
AB(20111225, a = true, b = true)
AB(20111226, a = true, b = false)
AB(20111227, a = false, b = true)
Currently:
I’m reading through file A, putting the datetime into listA:
listObjects = []
fileA = open(A.log, 'rb')
listA = []
for line in fileA:
try:
dt = datetime.strptime(line, "%Y%m%d")
except Exception, e:
continue
else:
listA.append(dt)
Then reading through file B, checking if each item exists in listA, and adding to listObjects the B-only and A+B items:
fileB = open(B.log, 'rb')
for line in fileB:
try:
dt = datetime.strptime(line, "%Y%m%d")
except Exception, e:
continue
else:
if dt in listA:
listA.remove(dt)
listObjects.append(AB(dt, a=True, b=True))
else:
listObjects.append(AB(dt, a=False, b=True))
I then loop through list A, adding the remaining A-only items:
for dt in listA:
listObjects.append(AB(dt, a=True, b=False))
Then sort the list by datetime:
shutdowns.sort(key=lambda x: x.datetime)
Is there a better way to do this? This seems a bit clunky, and as the length of the files increases it’ll get exponentially slower.
I know you can get the intersection of lists by using set(A) & set(B) – should I use this?
Yes, using sets is a good idea. Take
set(A)andset(B)then: