python - The best way to sort indexes in Text widget -
in text widget, indexes used point positions within text handled text widget. positions withing text in text widget marked using string indexes of form “line.column”. line numbers start @ 1, while column numbers start @ 0.
the problem how sort list of indexes? instance:
unsorted_indexes = ['1.30', '1.0', '1.25', '3.10', '3.100', '3.1', '3.8'] # expected result: ['1.0', '1.25', '1.30', '3.1', '3.8', '3.10', '3.100'] # cant sort strings: print(sorted(unsorted_indexes)) # gives incorrect values: ['1.0', '1.25', '1.30', '3.1', '3.10', '3.100', '3.8'] # cant convert float print(sorted([float(index) index in unsorted_indexes])) # [1.0, 1.25, 1.3, 3.1, 3.1, 3.1, 3.8]
if transform string index other form, off course need way original form. in above example doing float('3.1')
, float('3.100')
results in float values of 3.1
. cant return '3.1' or '3.100'. or maybe in tkinter there mechanism this, , i'm missing it?
hopes can you: here code:
>>> unsorted_indexes = ['1.30', '1.0', '1.25', '3.10', '3.100', '3.1', '3.8'] >>> unsorted_indexes.sort(key=lambda x: [x.split('.')[0], int(x.split('.')[1])]) >>> unsorted_indexes ['1.0', '1.25', '1.30', '3.1', '3.8', '3.10', '3.100'] >>>
Comments
Post a Comment