【发布时间】:2011-01-27 15:37:24
【问题描述】:
假设我有 2 个类似这样的列表:
L1=['Smith, John, 2008, 12, 10, Male', 'Bates, John, 2006, 1, Male', 'Johnson, John, 2009, 1, 28, Male', 'James, John, 2008, 3, Male']
L2=['Smith, Joy, 2008, 12, 10, Female', 'Smith, Kevin, 2008, 12, 10, Male', 'Smith, Matt, 2008, 12, 10, Male', 'Smith, Carol, 2000, 12, 11, Female', 'Smith, Sue, 2000, 12, 11, Female', 'Johnson, Alex, 2008, 3, Male', 'Johnson, Emma, 2008, 3, Female', 'James, Peter, 2008, 3, Male', 'James, Chelsea, 2008, 3, Female']
我想用它来比较一个家庭中每个人(姓氏相同)的日期与他们每个家庭中的“约翰”。日期从包括年、月和日,到只有年和月,再到只有年。我想找出约翰的日期和他每个家庭成员的日期之间的差异,直到我能做到的最具体的点(如果一个日期包含所有 3 个部分而另一个只有月份和年份,那么只找到月份和年份的时差)。这是我迄今为止尝试过的方法,但它不起作用,因为它没有使用正确的名称和日期(它只给了每个约翰一个兄弟姐妹),而且它计算日期之间时间的方式令人困惑和错误:
for line in L1:
type=line.split(',')
if len(type)>=1:
family=type[0]
if len(type)==6:
yearA=type[2]
monthA=type[3]
dayA=type[4]
sex=type[5]
print '%s, John Published in %s, %s, %s, %s' %(family, yearA, monthA, dayA, sex)
elif len(type)==5:
yearA=type[2]
monthA=type[3]
sex=type[4]
print '%s, John Published in %s, %s, %s' %(family, yearA, monthA, sex)
elif len(type)==4:
yearA=type[2]
sex=type[3]
print '%s, John Published in %s, %s' %(family, yearA, sex)
for line in L2:
if re.search(family, line):
word=line.split(',')
name=word[1]
if len(word)==6:
yearB=word[2]
monthB=word[3]
dayB=word[4]
sex=word[5]
elif len(word)==5:
yearB=word[2]
monthB=word[3]
sex=word[4]
elif len(word)==4:
yearB=word[2]
sex=word[3]
if dayA and dayB:
yeardiff= int(yearA)-int(yearB)
monthdiff=int(monthA)-int(monthB)
daydiff=int(dayA)-int(dayB)
print'%s, %s Published %s year(s), %s month(s), %s day(s) before/after John, %s' %(family, name, yeardiff, monthdiff, daydiff, sex)
elif not dayA and not dayB and monthA and monthB:
yeardiff= int(yearA)-int(yearB)
monthdiff=int(monthA)-int(monthB)
print'%s, %s Published %s year(s), %s month(s), before/after John, %s' %(family, name, yeardiff, monthdiff, sex)
elif not monthA and not monthB and yearA and yearB:
yeardiff= int(yearA)-int(yearB)
print'%s, %s Published %s year(s), before/after John, %s' %(family, name, yeardiff, sex)
我想最终得到一个看起来像这样的东西,如果可能的话,让程序能够区分兄弟姐妹是在之前还是之后出现的东西,并且只打印月份和日期,如果它们同时出现在两个比较日期:
Smith, John Published in 2008, 12, 10, Male
Smith, Joy Published _ year(s) _month(s) _day(s) before/after John, Female
Smith, Kevin Published _ year(s) _month(s) _day(s) before/after John, Male
Smith, Matt Published _ year(s) _month(s) _day(s) before/after John, Male
Smith, Carol Published _ year(s) _month(s) _day(s) before/after John, Female
Smith, Sue Published _ year(s) _month(s) _day(s) before/after John, Female
Bates, John Published in 2006, 1, Male
Johnson, John Published in 2009, 1, 28, Male
Johnson, Alex Published _ year(s) _month(s) _day(s) before/after John, Male
Johnson, Emma Published _ year(s) _month(s) _day(s) before/after John, Female
James, John Published in 2008, 3, Male
James, Peter Published _ year(s) _month(s) _day(s) before/after John, Male
James, Chelsea Published _ year(s) _month(s) _day(s) before/after John, Female
【问题讨论】:
-
将时间间隔表示为年+月+日并非易事。示例:2007-01-31 和 2008-03-01 之间有多少年、多少月和多少天?
-
这就是我希望程序在“约翰”和他的每个兄弟姐妹之间找出的内容,并查看兄弟姐妹是在之前还是之后。
-
之前/之后以及天数都不是问题。问题是将它们转换为年+月+日。
-
这是我应该使用 datetime 模块的东西吗?我从来没有听说过它,但我现在正在读一点。不知道如何将信息正确地输入那里
-
您想要的是相对增量。查看 datetimes 和 dateutil 模块:niemeyer.net/python-dateutil Dateutil 提供相对增量,将闰年和二月等因素考虑在内。 (即“未来 1 个月后的日期是什么?”或“这两个日期之间有多少个月和多少周”)