[2.5.1.1/dictionary] Change sorting order?

Gilles Ganault nospam at nospam.com
Fri Jan 22 09:38:29 EST 2010


On Fri, 22 Jan 2010 14:09:43 +0100, Jean-Michel Pichavant
<jeanmichel at sequans.com> wrote:
>Sorry, the code I provided produce this output:
>
>['1a', 'a', 'ac', 'av', 'b', 'c']
>['a', 'ac', 'av', 'b', 'c', '1a']
>['b', 'c', '1a', 'a', 'ac', 'av']
>['c', '1a', 'a', 'ac', 'av', 'b']
>['1a', 'a', 'ac', 'av', 'b', 'c']
>
>which is actually what you are searching for. I just messed up with my 
>ipython shell history :o)

Thanks for the help. I'm a Python newbie, and have a difficult time
understanding what the [] + [] line does :-/

I'll simplify things by using a list instead of a dictionary:

============
connected = []
connected.append("0test")
connected.append("aa")
connected.append("bb")
connected.append("cc")

for start in '1abcd':
    result = [name for name in connected if name[0] >= start] + [name
for name in connected if name[0] < start]
    print result
============
C:\>test.py
['aa', 'bb', 'cc', '0test']
['aa', 'bb', 'cc', '0test']
['bb', 'cc', '0test', 'aa']
['cc', '0test', 'aa', 'bb']
['0test', 'aa', 'bb', 'cc']
============

Pretty close to what I need to do but..

1. Why is the first iteration done twice?

2. How can I have just one line, save the character that I used as
starting point, increment it, and save it into a file so it can be
read the next time this program runs? For instance, let's say we used
"b" to start looking for items, I'll save "c" in a file for the next
time.

Thank you.



More information about the Python-list mailing list