python - How to order by date defaultdict(list) value? -
defaultdict(<type 'list'>, {'003': [('biology', 'a', '04/18/2013'), ('irdu', 'a' , '04/17/2013')], '002': [('biology', 'a', '03/01/2013'), ('math', 'c', '01/10/2 013'), ('math', 'c', '03/10/2013')], '001': [('biology', 'b', '05/01/2013'), ('l iterature', 'b', '03/02/2013'), ('math', 'a', '02/20/2013')]})
i want sort each key date , following output
defaultdict(<type 'list'>, {'003': [('irdu', 'a', '04/17/2013'), ('biology', 'a', '04/18/2013')], '002': [('math', 'c', '01/10/2013'), ('biology', 'a', '03/01/2013'), ('math', 'c', '03/10/2013')], '001': [('math', 'a', '02/20/2013'), ('literature', 'b', '03/02/2013'), ('biology', 'b', '05/01/2013')]})
i tried following did not work idea ?
accounts = defaultdict(list) sorteddata = sorted(accounts.iteritems(), key=operator.itemgetter(2))
i think want :
key = itemgetter(2) sorteddata = {} k, v in accounts.items(): v.sort(key=key) sorteddata[k] = v
or
sorteddata = {(k, list(sorted(v, key=key)) k, v in accounts.items()}
Comments
Post a Comment