Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!hq-usenetpeers.eweka.nl!hq-usenetpeers.eweka.nl!bcyclone01.am1.xlned.com!bcyclone01.am1.xlned.com!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:error': 0.03; 'essentially': 0.04; 'string': 0.09; '"__main__":': 0.09; '"c"': 0.09; '"r"': 0.09; '%s"': 0.09; '__name__': 0.09; 'converted': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'def': 0.12; 'wrote': 0.14; 'windows': 0.15; '"a"': 0.16; '"b"': 0.16; '"d"': 0.16; '"e"': 0.16; '"f"': 0.16; '"g"': 0.16; '"n"': 0.16; '"o"': 0.16; '"s"': 0.16; '"u"': 0.16; '"x"': 0.16; 'accepts': 0.16; 'as-is': 0.16; 'itself,': 0.16; 'message- id:@4ax.com': 0.16; 'outp': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'string:': 0.16; 'true:': 0.16; 'trying': 0.19; 'thu,': 0.19; 'version.': 0.19; 'example': 0.22; 'print': 0.22; 'url:home': 0.24; 'header:X-Complaints-To:1': 0.27; '(c)': 0.29; '2009': 0.29; "i'm": 0.30; '-0700,': 0.31; 'keys': 0.31; 'larry': 0.31; 'lists': 0.32; 'handled': 0.32; 'skip:c 30': 0.32; 'another': 0.32; 'problem': 0.35; 'but': 0.35; 'version': 0.36; 'sequence': 0.36; 'set.': 0.36; 'skip:> 10': 0.36; 'charset :us-ascii': 0.36; "i'll": 0.36; 'changing': 0.37; 'skip:o 20': 0.38; 'to:addr:python-list': 0.38; 'skip:- 10': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'break': 0.61; 'subject: ': 0.61; 'myself': 0.63; 'different': 0.65; 'skip:+ 10': 0.65; 'phone': 0.66; 'reserved.': 0.68; 'detail.': 0.68; '"j"': 0.84; 'intrigued': 0.84; '800': 0.91; 'numbers:': 0.91; 'subject:Lists': 0.91; 'received:108': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: I am out of trial and error again Lists Date: Thu, 23 Oct 2014 09:23:29 -0400 Organization: IISS Elusive Unicorn References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: adsl-108-73-116-102.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 6.00/32.1186 X-No-Archive: YES X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 67 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1414070619 news.xs4all.nl 2926 [2001:888:2000:d::a6]:35454 X-Complaints-To: abuse@xs4all.nl X-Received-Body-CRC: 1433575846 X-Received-Bytes: 5968 Xref: csiph.com comp.lang.python:79511 On Thu, 23 Oct 2014 00:10:28 -0700, Larry Hudson declaimed the following: >I know you are trying to explore lists here, but I found myself somewhat intrigued with the >problem itself, so I wrote a different version. This version does not use lists but only >strings. I'm just presenting it as-is to let you try to follow the logic, but if you ask, I'll >explain it in detail. It turns your long sequence of if's essentially into a single line -- >unfortunately 's' and 'z' have to be handled as special-cases, which turns that single line into >a six-line if/elif/else set. You might consider this line 'tricky', but I'll just say it's just >looking at the problem from a different viewpoint. BTW, this version accepts upper-case as well >as lower-case. isdigit() and isalpha() are standard string methods. > This is another example where changing data structures can simplify things a lot... -=-=-=-=-=-=- keys = { "a" : "2", "b" : "2", "c" : "2", # 1 is not assigned "d" : "3", "e" : "3", "f" : "3", "g" : "4", "h" : "4", "i" : "4", "j" : "5", "k" : "5", "l" : "5", "m" : "6", "n" : "6", "o" : "6", "p" : "7", "q" : "7", "r" : "7", "s" : "7", "t" : "8", "u" : "8", "v" : "8", "w" : "9", "x" : "9", "y" : "9", "z" : "9" } def textToNumber(inp): outp = [] for c in inp.lower(): outp.append(keys.get(c, c)) return "".join(outp) if __name__ == "__main__": while True: #NOTE: Python 2.7! aNumStr = raw_input("\nEnter Phone Number String: ") if not aNumStr: break print "Converted to all numbers: %s" % textToNumber(aNumStr) -=-=-=-=-=-=- Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\Wulfraed\Documents\Python Progs>Phone.py Enter Phone Number String: 1-800-getcharter Converted to all numbers: 1-800-4382427837 Enter Phone Number String: 1-800-leo laporte Converted to all numbers: 1-800-536 5276783 Enter Phone Number String: 1 800 callaprogrammer Converted to all numbers: 1 800 225527764726637 Enter Phone Number String: +1(800)Leo-Laporte Converted to all numbers: +1(800)536-5276783 Enter Phone Number String: C:\Users\Wulfraed\Documents\Python Progs> -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/