Pythonic mapping of an array (Beginner)

Posted by scott_karana on Stack Overflow See other posts from Stack Overflow or by scott_karana
Published on 2010-04-05T19:58:20Z Indexed on 2010/04/05 20:23 UTC
Read the original article Hit count: 508

Filed under:
|
|

Hey StackOverflow, I've got a question related to a beginner Python snippet I've written to introduce myself to the language. It's an admittedly trivial early effort, but I'm still wondering how I could have written it more elegantly.

The program outputs NATO phoenetic readable versions of an argument, such "H2O" -> "Hotel 2 Oscar", or (lacking an argument) just outputs the whole alphabet. I mainly use it for calling in MAC addresses and IQNs, but it's useful for other phone support too.

Here's the body of the relevant portion of the program:

#!/usr/bin/env python

import sys

nato = {
"a": 'Alfa',
"b": 'Bravo',
"c": 'Charlie',
"d": 'Delta',
"e": 'Echo',
"f": 'Foxtrot',
"g": 'Golf',
"h": 'Hotel',
"i": 'India',
"j": 'Juliet',
"k": 'Kilo',
"l": 'Lima',
"m": 'Mike',
"n": 'November',
"o": 'Oscar',
"p": 'Papa',
"q": 'Quebec',
"r": 'Romeo',
"s": 'Sierra',
"t": 'Tango',
"u": 'Uniform',
"v": 'Victor',
"w": 'Whiskey',
"x": 'Xray',
"y": 'Yankee',
"z": 'Zulu',
}

if len(sys.argv) < 2:
    for n in nato.keys():
            print nato[n]
else:
    # if sys.argv[1] == "-i" # TODO
    for char in sys.argv[1].lower():
            if char in nato:
                    print nato[char],
            else: print char,

As I mentioned, I just want to see suggestions for a more elegant way to code this. My first guess was to use a list comprehension along the lines of [nato[x] for x in sys.argv[1].lower() if x in nato], but that doesn't allow me to output any non-alphabetic characters. My next guess was to use map, but I couldn't format any lambdas that didn't suffer from the same corner case.

Any suggestions? Maybe something with first-class functions? Messing with Array's guts?
This seems like it could almost be a Code Golf question, but I feel like I'm just overthinking :)

© Stack Overflow or respective owner

Related posts about python

Related posts about elegance