[2.5.1.1/dictionary] Change sorting order?

Jan Kaliszewski zuo at chopin.edu.pl
Fri Jan 22 10:34:11 EST 2010


PS.

22-01-2010 o 15:44:28 Jan Kaliszewski <zuo at chopin.edu.pl> wrote:

> 22-01-2010, 14:58:58 Gilles Ganault <nospam at nospam.com> wrote:
>
>> On 22 Jan 2010 13:35:26 GMT, Neil Cerutti <neilc at norwich.edu> wrote:
>>> Resorting is more work than is needed. Just choose a different
>>> starting index each time you display the names, and set up your
>>> lister to wrap-around to your arbitrary starting index.
>>
>> Thanks. In this case, it means that in each loop iteration, I must
>> search the list to find which item starts with the letter I'd like to
>> begin sorting, eg. "B". Does Python include a search method or do I
>> have to use a for loop to locate this starting item?
>
> There is e.g. `bisect' module -- you can search as well as insert with
> its functions. But IMHO you shouldn't search for the next starting
> *letter*, but for the next *name* in the list (basing on name that was
> used recently).

Or simply choose the starting index randomly -- using  
random.randrange(len(userlist)) or random.randint(0, len(userlist)-1) --  
and use bisect.insort() for inserting to the list keeping it sorted (if  
you need to list users sorted+rotated).

Regards,
*j



More information about the Python-list mailing list