[Tutor] creating dictionary from a list
Mark Lawrence
breamoreboy at yahoo.co.uk
Sat Apr 13 20:29:00 CEST 2013
On 13/04/2013 19:19, Saad Javed wrote:
>
>
> Don't fight Python, unlike this chap[1] :) Basically if you're
> looping around any data structure you rarely need to use indexing,
> so try this approach.
>
> for item in lst:
> if
> item.startswith(('Mon','Tue','__Wed','Thu','Fri','Sat','Sun'))__:
> myDict[item] = []
> saveItem = item
> else:
> myDict[saveItem].append(item.__strip())
>
>
> This creates a dictionary whose keys are out of order in terms of dates.
>
> {'Thu Apr 04': ['Weigh In'], 'Sat Apr 06': ['Collect NIC', 'Finish PTI
> Video'], 'Wed Apr 10': ['Serum uric acid test'], 'Sun Apr 14':
> ['Download Louis CK Oh My God', '4:00pm', 'UPS Guy'], 'Sat Apr 13':
> ['1:00pm', 'Get flag from dhariwal']}
>
> Sat Apr 13 is appearing after Sun Apr 14. How do you sort this?
>
http://docs.python.org/3/library/collections.html#collections.OrderedDict
--
If you're using GoogleCrap™ please read this
http://wiki.python.org/moin/GoogleGroupsPython.
Mark Lawrence
More information about the Tutor
mailing list