473,386 Members | 1,841 Online

# Q: listsort and dictsort - official equivalents?

I expressed my creativity & created two routines, listsort & dictsort.

def listsort(l,cmp=None): l.sort(cmp); return l
def dictsort(d,cmp=None):return [(k,d[k])for k in listsort(d.keys(),cmp=cmp)]

Basically I am more familiar with sorting inside a for loop, eg in
bourne shell one can do "for loop sorts":

for group in \$(sort /etc/group); do
echo group;
done

In python I must kick off a sort on the line before I start the
iteration. (This does make sense because at the end of the day the sort
has complete BEFORE the for loop can proceed - that is... until the day
when python lists have a secondary index ;-).

group_list=group_dict.keys()
group_list.sort()
for group in group_list: # do
print group,group_dict[group]
# done

I am sure python has a more concise way of doing this, any hints?

Cheers
NevilleD

# examples:

#!/usr/bin/env python
## -*- coding: utf-8 -*-
## purpose: demo sorting in python
def aleph_cmp(a,b): # class only needs the "<" operator
if a<b: return -1
elif b<a: return 1
else: return 0 # fi
# end aleph_cmp

def listsort(l,cmp=None): l.sort(cmp); return l # end

# example application:
mydict=dict(zip("cba",(2,1,3)))

print "a) sort by the first key using the std cmp"
for key in listsort(mydict.keys(),cmp=cmp): # do
print key,mydict[key]
# od

# if your object had a '-' operator, then you would not need to do this.
print "b) custom sort by the first key - need the '<' operator"
for key in listsort(mydict.keys(),aleph_cmp): # do
print key,mydict[key]
# od

print "c) custom sort by the second key - using the numeric '-' operator"
for key in listsort(mydict.keys(),cmp=lambda a,b: mydict[a]-mydict[b]): # do
print key,mydict[key]
# od

# α*inally: I αelieve listsort & dictsort aα±e not in the αtd... αut αayα they α±?
def dictsort(d,cmp=None):return [(k,d[k])for k in listsort(d.keys(),cmp=cmp)] # end

print "d) 'dictsort' sort by the second key - using the numeric '-' operator"
for key,val in dictsort(mydict,cmp=lambda a,b: mydict[a]-mydict[b]): # do
print key,val
# od

Am I missing some obvious standard routine?
--
Neville D - NZ
α*αΎαα¬βα‘αααα¬βα‘ααααα *αααͺαα’αα¬βαͺα¬βααα’αα¬β αΎα°α¬βαααα±αααͺαα*
note: This document contains Anglo-Saxon Futhorci UTF-8 characters
from: http://junicode.sourceforge.net/ - eg α¦α α*αα±αα α*α°α

Jun 20 '07 #1
0 934

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 3 by: Simba | last post by: Hi, I've written some javascript routines for Internet Explorer and Opera. Could you tell me the Netscape and Mozilla equivalents? Thanks ------------------------- //this works with... 37 by: Jeff Thies | last post by: There's a number of elements that I set attributes for: .. Are there CSS equivalents for any of these? Jeff 1 by: iGadget | last post by: Hi, Is anyone aware of a built-in method to substitute HTML or XML reserved characters with their escaped equivalents? If I have the String "If X < Y then hello" I have to substitute the '<' in... 6 by: Harry Strybos | last post by: Hi All Is there a book or web site that lists all the VB.Net equivalents for VB.6 methods? In other words, a list of all VB6 keywords and their equivalent in VB.Net. This would be of enormous... 0 by: taylorcarr | last post by: A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,... 0 by: Charles Arthur | last post by: How do i turn on java script on a villaon, callus and itel keypad mobile phone 0 by: aa123db | last post by: Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function \$name\$ (\$parameters\$) { } ... 0 by: ryjfgjl | last post by: If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming... 0 by: BarryA | last post by: What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress... 1 by: nemocccc | last post by: hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions? 0 by: Hystou | last post by: There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2... 0 by: marktang | last post by: ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,... 0 by: jinu1996 | last post by: In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...