Python Create List Of Objects From Multiple Files Showing Intersection
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 __ini
Solution 1:
Yes, using sets is a good idea. Take set(A)
and set(B)
then:
list_of_objects = [(i, i in A, i in B) for i inset(A) | set(B)]
Solution 2:
Not going into the detail as to how you are reading the file, lets create two sets of date-times present in fileA and fileB as follows
setA=set(datetime.strptime(a, "%Y%m%d") for a in fileA)
setB=set(datetime.strptime(b, "%Y%m%d") for a in fileB)
Now Create the set of elements present in both the files
setAB=setA.intersection(setB)
Now create the remaining two sets which would contain elements which are exclusively present in fileA and fileB
setAOnly=setA-setAB
setBOnly=setB-setAB
Now finally create the list of objects. Note now that we have classified the elements into three different sets, we don't need to search but rather can hard code the values a
and b
in the class AB.
[AB(d,True,False) for d in setAOnly] + \
[AB(d,False,True) for d in setBOnly] + \
[AB(d,True,True) for d in setAB]
Post a Comment for "Python Create List Of Objects From Multiple Files Showing Intersection"