Python从显示交集的多个文件创建对象列表 [英] Python create list of objects from multiple files showing intersection

查看:162
本文介绍了Python从显示交集的多个文件创建对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从两个文件(数据时间列表)中读取,称为A和B.(不一定按顺序)

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

如果datetime存在于文件A中,则a应设置为true,如果datetime存在于文件B中,则应设置为b true(否则为false)

a should be set to true if the datetime exists in file A, and b true if datetime exists in file B. (false otherwise)

例如:


档案A:20111225,20111226

文件B:20111225,20111227

file A: 20111225, 20111226
file B: 20111225, 20111227

对象列表应为:


AB(20111225,a = true,b = true)

AB(20111226,a = true,b = false)

AB(20111227,a = false,b = true)

AB(20111225, a = true, b = true)
AB(20111226, a = true, b = false)
AB(20111227, a = false, b = true)

目前

我正在读取文件A,将datetime放入listA:

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)

然后读取文件B,检查listA中是否存在每个项目,并向listObjects添加仅B和A + B项:

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))

然后我遍历列表A,添加剩余的只有A的项目:

I then loop through list A, adding the remaining A-only items:

for dt in listA:
    listObjects.append(AB(dt, a=True, b=False))

然后按datetime对列表排序:

Then sort the list by datetime:

shutdowns.sort(key=lambda x: x.datetime)



< hr>

有更好的方法吗?这看起来有点笨重,并且随着文件的长度增加,它将指数地减慢。


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.

我知道你可以通过使用set )&设置(B) - 我应该使用这个吗?

I know you can get the intersection of lists by using set(A) & set(B) - should I use this?

推荐答案

是的,使用集合是一个好主意。以设置(A)设置(B)

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)]

这篇关于Python从显示交集的多个文件创建对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆