Pages

Tuesday, August 08, 2006

Python

Non-computer-programmers: look away now. Python is a programming language as well as a snake.

I've been getting to know Python over the last few months, and I just love how easy some things are in it, being a truely object-oriented and pretty dynamically-typed scripting language. For instance, to turn a string (or list or tuple) of letters that are codes for particular words into a comma-separated list of the longer format:

codes = "DBC" # for example mappingdict = {'A':'Alpha', 'B':'Bravo', 'C':'Charlie', 'D':'Delta', 'E':'Echo'} print ",".join(map(mappingdict.get, codes)) Will print "Delta,Bravo,Charlie". How easy is that! Tags: rave

2 comments:

  1. message = 'abc'
    code = {:a => "Alpha", :b => "Baker", :c => "Charlie"}
    message.split(//).map{|m| code[m.to_sym]}

    ReplyDelete
  2. That last comment by Jon is the Ruby version, if you're interested.

    ReplyDelete