Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'string.': 0.04; 'modify': 0.05; '%s"': 0.07; 'subject:code': 0.07; 'input,': 0.09; 'occurrences': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; '"%s': 0.16; 'checks:': 0.16; 'in-place': 0.16; 'looping': 0.16; 'occurences': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'string': 0.17; 'items.': 0.17; 'skip': 0.17; 'string,': 0.17; 'subject:Issue': 0.17; '>>>': 0.18; 'input': 0.18; 'feb': 0.19; 'trying': 0.21; 'converted': 0.22; 'occurs': 0.22; 'besides': 0.27; "doesn't": 0.28; 'header:X -Complaints-To:1': 0.28; 'rest': 0.28; 'character': 0.29; "i'm": 0.29; '(and': 0.32; 'print': 0.32; 'shift': 0.33; 'url:home': 0.33; 'to:addr:python-list': 0.33; 'code:': 0.33; 'hi,': 0.33; 'list': 0.35; 'there': 0.35; 'next': 0.35; 'received:org': 0.36; 'but': 0.36; 'subject:with': 0.36; 'charset:us-ascii': 0.36; 'turn': 0.36; 'does': 0.37; 'why': 0.37; 'rather': 0.37; 'subject:: ': 0.38; 'fact': 0.38; 'to:addr:python.org': 0.39; 'list,': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'remove': 0.61; 'times': 0.63; 'here': 0.65; 'reverse': 0.65; 'counts': 0.81; "'2',": 0.84; "'3',": 0.84; '2013': 0.84; 'dennis': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: Issue with my code Date: Tue, 05 Feb 2013 17:20:09 -0500 Organization: > Bestiaria Support Staff < References: <2b0eb097-e575-4d6d-a509-f4c6bd58c934@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: adsl-76-249-22-149.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 3.3/32.846 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: 85 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360102819 news.xs4all.nl 6911 [2001:888:2000:d::a6]:60303 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38238 On Tue, 5 Feb 2013 10:38:55 -0800 (PST), maiden129 declaimed the following in gmane.comp.python.general: > Hi, > > I'm trying to create this program that counts the occurrences of each digit in a string which the user have to enter. > > Here is my code: > > s=input("Enter a string, eg(4856w23874): ") > s=list(s) > > checkS=['0','1','2','3','4','5','6','7','8','9'] > > for i in s: > if i in checkS: > t=s.count(i) Let's see... for each character in the input, see if that character is in the check list, and if it is, then reverse and count the occurences in the input string. (Oh, that's after you converted the input to a list of single characters) > if t>1: > for k in range(1,t): > s=s.remove(i) > print(i, "occurs", t,"times.") Oh Oh.... Besides the fact that .remove() does its work in-place (and doesn't return a value -- hence the "s=" is binding None to s), you are trying to modify the string that you are looping over... And that is a no-no -- it will cause you to skip over items. -=-=-=-=-=- >>> s = list("123456789") >>> for c in s: ... if c == "3": ... s.remove(c) ... print "c: %s\tREMOVED" % (c,) ... else: ... print "c: %s\ts: %s" % (c, s) ... c: 1 s: ['1', '2', '3', '4', '5', '6', '7', '8', '9'] c: 2 s: ['1', '2', '3', '4', '5', '6', '7', '8', '9'] c: 3 REMOVED c: 5 s: ['1', '2', '4', '5', '6', '7', '8', '9'] c: 6 s: ['1', '2', '4', '5', '6', '7', '8', '9'] c: 7 s: ['1', '2', '4', '5', '6', '7', '8', '9'] c: 8 s: ['1', '2', '4', '5', '6', '7', '8', '9'] c: 9 s: ['1', '2', '4', '5', '6', '7', '8', '9'] >>> -=-=-=-=-=- Note how there is no line for c=4. When you remove the "3", all the rest shift left to fill in -- but the next round of the loop is going to "increment" to the character after the position that "3" was at... That is now the "5". Rather than working from the input list, why not turn it around... -=-=-=-=-=- >>> inpt = "4856w2304874" >>> digits = "0123456789" >>> for d in digits: ... o = inpt.count(d) ... if o: ... print "%s occurs %s times" % (d, o) ... 0 occurs 1 times 2 occurs 1 times 3 occurs 1 times 4 occurs 3 times 5 occurs 1 times 6 occurs 1 times 7 occurs 1 times 8 occurs 2 times >>> -=-=-=-=-=- -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/