import itertools as IT
items = ['Thu Apr 04', ' Weigh In', 'Sat Apr 06', ' Collect NIC', ' Finish PTI Video', 'Wed Apr 10', ' Serum uric acid test', 'Sat Apr 13', ' 1:00pm', 'Get flag from dhariwal', 'Sun Apr 14', ' Louis CK Oh My God', ' 4:00pm', 'UPS Guy']
date_word = ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun')
def isdate(datestring):
return any(datestring.startswith(d) for d in date_word)
items = (item.strip() for item in items)
data = (list(group) for key, group in IT.groupby(items, key=isdate))
for date, items in IT.izip(*[data]*2):
print('{d} {i}'.format(d=date[0], i=items))
yields
Thu Apr 04 ['Weigh In']
Sat Apr 06 ['Collect NIC', 'Finish PTI Video']
Wed Apr 10 ['Serum uric acid test']
Sat Apr 13 ['1:00pm', 'Get flag from dhariwal']
Sun Apr 14 ['Louis CK Oh My God', '4:00pm', 'UPS Guy']
-
You could use IT.groupby to group the items as desired.
-
If you don’t don’t dump the items into a dict, you can preserve the order in which the items appear.
-
You can use the
zip(*[iterator]*2)
grouper recipe to group items in pairs. -
Avoid using variable names like
dict
since they shadow the Python builtin
object of the same name.
11
solved python list to dictionary with dates as keys [closed]