(datetime.date(2007,2,11),datetime.date(2007,2,24)) ] for几个月: candidate_periods = [p为句子中的p,如果m [0]< = p [0]和p [1]< = m [1]] 如果candidate_periods: print m [0]," =>",min(candidate_periods) else: print m [0]," =>" ;,没有期间匹配 < / code> TJGWell, I can come up with a solution which basically reflects theway I''d do it in SQL (since this kind of thing is my bread-and-butterthere) but I''m not convinced it''s really any better than your proposal.However, for the purposes of illustration:<code>import calendarimport datetimeYEAR = 2007months = [ (datetime.date (YEAR, n, 1), datetime.date (YEAR, n, calendar.monthrange (YEAR, n)[1])) for n in range (1, 13)]periods = [ (datetime.date(2006, 12, 31), datetime.date(2007, 1, 13)), (datetime.date(2007, 1, 14), datetime.date(2007, 1, 27)), (datetime.date(2007, 1, 28), datetime.date(2007, 2, 10)), (datetime.date(2007, 2, 11), datetime.date(2007, 2, 24))]for m in months: candidate_periods = [p for p in periods if m[0] <= p[0] and p[1] <= m[1]] if candidate_periods: print m[0], "=>", min (candidate_periods) else: print m[0], "=>", "no period matches"</code>TJG 谢谢!我会尝试两种方式,看看是否有任何明显的速度差异,但因为它将被打包成一个 可执行文件,这可能不会无论如何都是一个问题。 迈克Thanks! I''ll try it both ways and see if there''s any appreciabledifference in speed, although since it will be packaged into anexecutable, that may not be an issue anyway.Mike 我真的怀疑有没有对我的方法有利,当然不是速度方面的b $ b。它真的只是恰好适合你的想法更好,或者利用你可能已经拥有的结构,等等。 TJGI honestly doubt there''s any advantage to my approach, certainlynot in terms of speed. It''s really only if it happens to suityour mind better, or take advantage of structures you mightalready have in place, etc.TJG 这篇关于查找特定日期范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-23 00:06
查看更多