Python create list of objects from multiple files showing intersection

拥有回忆 提交于 2019-12-25 08:04:40

问题


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?


回答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 in set(A) | set(B)]



回答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]


来源:https://stackoverflow.com/questions/8753074/python-create-list-of-objects-from-multiple-files-showing-intersection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!