14 May 21:01
basic lists and loops question
From: Jon Crump <jjcrump <at> myuw.net>
Subject: basic lists and loops question
Newsgroups: gmane.comp.python.tutor
Date: 2008-05-14 19:02:20 GMT
Subject: basic lists and loops question
Newsgroups: gmane.comp.python.tutor
Date: 2008-05-14 19:02:20 GMT
Something basic about lists and loops that I'm not getting here. I've got
a long list of dictionaries that looks like this:
lst = [{'placename': u'Stow, Lincolnshire', 'long-name': u'Stow,
Lincolnshire.', 'end': datetime.date(1216, 9, 28), 'start':
datetime.date(1216, 9, 26)},
{'placename': u'Lincoln, Lincolnshire', 'long-name': u'Lincoln,
Lincolnshire.', 'end': datetime.date(1216, 9, 30), 'start':
datetime.date(1216, 9, 28)},
{'placename': u'Lincoln, Lincolnshire', 'long-name': u'Lincoln,
Lincolnshire.', 'end': datetime.date(1216, 10, 2), 'start':
datetime.date(1216, 10, 1)},
{'placename': u'Grimsby, Lincolnshire', 'long-name': u'Grimsby,
Lincolnshire.', 'end': datetime.date(1216, 10, 4), 'start':
datetime.date(1216, 10, 3)},
{'placename': u'Louth, Lincolnshire', 'long-name': u'Louth,
Lincolnshire.', 'end': datetime.date(1216, 10, 4), 'start':
datetime.date(1216, 10, 4)}
]
I have a function that searches through them to find pairs of dictionaries
that satisfy certain criteria. When the nested loops find such a pair, I
need to merge them. So far so good. This works:
def events(data):
evts = []
for x in lst:
for y in lst:
if (x['placename'] == y['placename']) and (x['end'].month + 1 ==
y['start'].month) and (y['start'] - x['end'] == datetime.timedelta(1)):
(Continue reading)
RSS Feed