[2.5.1.1/dictionary] Change sorting order?
Duncan Booth
duncan.booth at invalid.invalid
Fri Jan 22 10:24:58 EST 2010
Jean-Michel Pichavant <jeanmichel at sequans.com> wrote:
> Here is one possible solution
>
> l = ['1a', 'a', 'b','c','av','ac'] # you mentioned a dictionary in your
> post, if so, l = myDict.keys()
> l.sort() # sort your list once and for all
> for start in '1abcd':
> result = [name for name in l if name[0] >= start] + [name for name
> in l if name[0] < start]
> print result
Here's another:
>>> import bisect
>>> def rotated_sort(data, startch):
data = sorted(data)
pos = bisect.bisect_left(data, startch)
return data[pos:] + data[:pos]
>>> for ch in " 1abcd":
print ch, rotated_sort(['1a', 'a', 'b','c','av','ac'], ch)
['1a', 'a', 'ac', 'av', 'b', 'c']
1 ['1a', 'a', 'ac', 'av', 'b', 'c']
a ['a', 'ac', 'av', 'b', 'c', '1a']
b ['b', 'c', '1a', 'a', 'ac', 'av']
c ['c', '1a', 'a', 'ac', 'av', 'b']
d ['1a', 'a', 'ac', 'av', 'b', 'c']
>>>
--
Duncan Booth http://kupuguy.blogspot.com
More information about the Python-list
mailing list