Take for example
'green',100
'blue',100
'orange',3
'blue',10
'orange',11
we could sort this spread sheet by either the color column or the number column.
Using the following function-
def sorting_hat(a_list,column): col=column bycolumn=lambda x:x[col] from itertools import groupby a_list=sorted(a_list,key=bycolumn) cat=[] gr=[] for c,g in groupby(a_list,key=bycolumn): cat.append(c) gr.append(list(g)) a_list=gr return a_list,catwe can sort by color (column 0)
info=[['green',100], ['blue',100], ['orange',3], ['blue',10], ['orange',11]] sortedlist,categories=sorting_hat(info,0) >>> categories ['blue', 'green', 'orange'] >>> sortedlist [[['blue', 100], ['blue', 10]], [['green', 100]], [['orange', 3], ['orange', 11]]]
and we can sort by number (column 1)
info=[['green',100], ['blue',100], ['orange',3], ['blue',10], ['orange',11]] sortedlist,categories=sorting_hat(info,1) >>> categories [3, 10, 11, 100] >>> sortedlist [[['orange', 3]], [['blue', 10]], [['orange', 11]], [['green', 100], ['blue', 100]]]
No comments:
Post a Comment