After posting I went out, and while out I kept thinking about that function and got a new idea. I didn't realize until I got back that I had completely screwed up my first attempt. Anyway, here's my second, which (I think) does the job for lists of 2-element tuples:

=======================================================
def sort_tuple_list_by_2nd_elements(alist):
    alist.sort
    alist_tup_elements_reversed = []
    for x in alist:
        alist_tup_elements_reversed.append((x[1], x[0]))
    alist_tup_elements_reversed.sort()
    print alist_tup_elements_reversed
   
    alist_tup_elements_reversed_and_reversed_again = []
    for x in alist_tup_elements_reversed:
        alist_tup_elements_reversed_and_reversed_again.append((x[1], x[0]))
    return alist_tup_elements_reversed_and_reversed_again


simpler_list = [('a', 1), ('c', 8), ('d', 1), ('g', 6), ('f', 6), ('h', 10)]
colors = [('khaki4', (139, 134, 78)), ('antiquewhite', (250, 235, 215)), ('cyan3', (0, 205, 205)), ('antiquewhite1', (238, 223, 204)), ('dodgerblue4', (16, 78, 139)), ('antiquewhite4', (139, 131, 120)),]

print sort_tuple_list_by_2nd_elements(simpler_list)
print
print sort_tuple_list_by_2nd_elements(colors)
"""
OUTPUT
[('a', 1), ('d', 1), ('f', 6), ('g', 6), ('c', 8), ('h', 10)]

[('cyan3', (0, 205, 205)), ('dodgerblue4', (16, 78, 139)), ('antiquewhite4', (139, 131, 120)), ('khaki4', (139, 134, 78)), ('antiquewhite1', (238, 223, 204)), ('antiquewhite', (250, 235, 215))]
"""
===========================================================

I thank Jerry Hill and Kent Johnson for their kind replies. I haven't figured them out yet, but I will (I think).  :-)

Dick
_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to