473,386 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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;

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 ;-).

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?


# 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:

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

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...
by: Jeff Thies | last post by:
There's a number of elements that I set attributes for: <table border="1" cellpadding="3" cellspacing="0">.. <img align="middle"> Are there CSS equivalents for any of these? Jeff
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...
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...
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,...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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$) { } ...
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...
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...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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,...
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.