| |||
Sorting and subsorting into your own order
More on Collections and Sequences example from a Well House Consultants training course
More on More on Collections and Sequences [link]
Source code: rails_dict Module: Y111
# Sorting and subsorting into your own order
import re class station: def __init__(this,text): bits = text.strip().split("\t") letters = re.compile('[A-Z]+') this.zone = letters.findall(bits[2]) this.postcode = bits[2] this.name = bits[6] this.code = bits[1] this.osref = bits[3] this.byyear = bits[7:] def getsuperzone(this): if len(this.zone) < 1: return "absent" return this.zone[0] def getcode(this): return this.code def getosref(this): return this.osref def getyear(this,which): return this.byyear[which-2005] def getname(this): return this.name def namematch(this,wanted): if this.name.find(wanted) >= 0: return True return False def small(this): if this.byyear[-1] == "NULL": return False if int(this.byyear[-1]) > 1000: return False return True # 1975 BUD LA9 6RD SD 502958 54.355307679 -2.7662155197 # Burneside 7759 9392 9561 12394 13776 16112 def bynumber (home, away): fred = cmp(count[home],count[away]) if fred == 0: fred = cmp(home,away) return fred fh = open("railstats.xyz") count = {} for place in fh.xreadlines(): current = station(place) pctop = current.getsuperzone() previous = count.get(pctop,0) count[pctop] = previous + 1 kz = count.keys() kz.sort(bynumber) for zone in kz: print zone, count[zone] """ Here's how it runs: munchkin:ps grahamellis$ python rails_dict DS 1 GH 1 LH 1 P 1 TD 1 WC 1 absent 1 DH 2 SR 2 HR 3 HX 4 IG 4 NN 4 TA 4 TF 4 HG 5 SN 5 DG 6 LN 6 LU 6 SP 6 TQ 6 WV 6 AB 8 EC 8 UB 8 DL 9 DY 9 GL 9 HU 9 WR 9 DD 10 DT 10 FY 10 AL 11 BL 11 LE 11 WD 11 BH 12 FK 12 HD 12 KW 12 SM 12 WN 12 HA 13 LD 13 ST 13 W 13 WS 13 CB 14 DE 14 MK 14 RM 14 BA 15 CW 15 OL 15 PR 15 CV 16 NP 17 SL 17 BD 18 HP 18 PE 18 SG 18 TR 18 KY 19 NULL 19 SS 19 SY 19 EN 20 IP 20 OX 20 WF 20 BB 21 DA 21 TS 21 BR 22 CR 22 NE 22 CM 23 CO 23 LS 23 BS 24 NW 24 TW 24 CA 25 E 25 ML 25 PH 26 KA 27 PL 27 PO 27 WA 27 CT 28 EH 28 IV 28 SW 28 YO 28 SO 29 LA 31 N 31 NR 31 PA 31 RG 31 EX 32 S 32 ME 33 GU 34 RH 35 NG 36 BN 39 DN 39 KT 39 M 40 SA 41 CH 42 SK 48 L 50 TN 54 SE 55 LL 60 B 69 CF 75 G 109 munchkin:ps grahamellis$ """ Learn about this subject
This module and example are covered on the following public courses:
* Learning to program in Python * Python Programming * Intermediate Python Also available on on site courses for larger groups Books covering this topic
Yes. We have over 700 books in our library. Books
covering Python are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "More on Collections and Sequences" training module. You'll find a description of the topic and some
other closely related examples on the "More on Collections and Sequences" module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License. Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre. • The Horse's mouth provides a daily tip or thought. • Further resources are available via the resources centre. • All of these resources can be searched through through our search engine • And there's a global index here. Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Web site author
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job. Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details. Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request. | |||
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho PAGE: http://www.wellho.net/resources/ex.php4 • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb |