我有两个文件加载到 Python 中,我正在尝试输出一个列表,该列表将显示两者之间的共同值。
第一个文件的列表如下所示(不是完整列表,只是其中的一部分):
[datetime.datetime(2010, 7, 30, 12, 20, 19, 143000), datetime.datetime(2010, 7, 30, 12, 22, 33, 631000), datetime.datetime(2010, 7, 30, 12, 22, 41, 236000), datetime.datetime(2010, 7, 30, 12, 23, 43, 547000), datetime.datetime(2010, 7, 30, 12, 23, 57, 453000), datetime.datetime(2010, 7, 30, 12, 26, 4, 713000), datetime.datetime(2010, 7, 30, 12, 26, 9, 46000), datetime.datetime(2010, 7, 30, 12, 28, 30, 313000)]
第二个列表看起来像这样(同样,不是完整列表,只是其中的一部分):
[datetime.datetime(2010, 7, 30, 13, 43, 2, 993000), datetime.datetime(2010, 7, 30, 13, 43, 10, 917000), datetime.datetime(2010, 7, 30, 13, 48, 56, 697000), datetime.datetime(2010, 7, 30, 13, 49, 14, 399000), datetime.datetime(2010, 7, 30, 13, 51, 45, 882000), datetime.datetime(2010, 7, 30, 13, 52, 6, 432000), datetime.datetime(2010, 7, 30, 13, 54, 26, 873000), datetime.datetime(2010, 7, 30, 13, 59, 2, 164000), datetime.datetime(2010, 7, 30, 13, 59, 15, 515000), datetime.datetime(2010, 7, 30, 14, 3, 43, 742000), datetime.datetime(2010, 7, 30, 14, 5, 59, 975000), datetime.datetime(2010, 7, 30, 14, 13, 36, 887000), datetime.datetime(2010, 7, 30, 14, 13, 42, 92000)]
这是代码的样子:
for infilelines in text:
lspl1 = infilelines.split(',')
cattime = lspl1[2]
catdate.append(datetime.strptime(lspl1[1]+cattime,'%d/%m/%Y%H:%M:%S.%f'))
#print catdate
NNSRCfile = sf.readsrc(NNSRC)
l1 = NNSRCfile['date']
l2 = NNSRCfile['time']
NNSRCfile['datetime2'] = zip(l1,l2)
#print NNSRCfile['datetime2']
NNSRCfile['datetimenew'] = [datetime.strptime(i+j,'%m-%d-%Y%H:%M:%S.%f') for i,j in zip(l1,l2)]
#print NNSRCfile['datetimenew']
ii = 0
while ii <= len(catdate):
try:
#print NNSRCfile['datetimenew'].index(catdate[ii]), "WE FOUND A MATCH!!!!"
indices = NNSRCfile['datetimenew'].index(catdate[ii])
except:
print "No match."
#continue
ii += 1
我没有收到错误,但脚本没有按照我的预期工作。我想要两个单独列表之间所有共同元素的完整列表。我知道我没有在这篇文章中包含完整的列表,但是当我手动查看它时,我知道两者之间有共同点。
任何帮助是极大的赞赏!