[2.5.1.1/dictionary] Change sorting order?
Gilles Ganault
nospam at nospam.com
Fri Jan 22 12:43:00 EST 2010
On 22 Jan 2010 15:24:58 GMT, Duncan Booth
<duncan.booth at invalid.invalid> wrote:
>Here's another:
Thanks for the sample. It work great, except that it also runs when
the header character doesn't match any item in the list:
=======
import bisect
connected = []
connected.append("_test")
connected.append("-test")
connected.append("0test")
connected.append("1test")
connected.append("Aa")
connected.append("Bb")
connected.append("Cc")
def rotated_sort(data, startch):
data = sorted(data)
pos = bisect.bisect_left(data, startch)
return data[pos:] + data[:pos]
for ch in "_-0123456789ABCDEFGHIJKLMNOPQRSTUVWYZ":
print ch, rotated_sort(connected, ch)
=======
C:\>test.py
_ ['_test', '-test', '0test', '1test', 'Aa', 'Bb', 'Cc']
- ['-test', '0test', '1test', 'Aa', 'Bb', 'Cc', '_test']
0 ['0test', '1test', 'Aa', 'Bb', 'Cc', '_test', '-test']
1 ['1test', 'Aa', 'Bb', 'Cc', '_test', '-test', '0test']
2 ['Aa', 'Bb', 'Cc', '_test', '-test', '0test', '1test']
3 ['Aa', 'Bb', 'Cc', '_test', '-test', '0test', '1test']
4 ['Aa', 'Bb', 'Cc', '_test', '-test', '0test', '1test']
=======
Should I add an "if" block in the "for ch in"?
More information about the Python-list
mailing list