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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'elif': 0.04; 'subject:code': 0.07; '(although': 0.09; 'received:internal': 0.09; 'checks:': 0.16; 'message- id:@webmail.messagingengine.com': 0.16; 'received:10.202': 0.16; 'received:10.202.2': 0.16; 'received:10.202.2.44': 0.16; 'received:66.111': 0.16; 'received:66.111.4': 0.16; 'received:compute4.internal': 0.16; 'received:mail.srv.osa': 0.16; 'received:messagingengine.com': 0.16; 'received:nyi.mail.srv.osa': 0.16; 'received:osa': 0.16; 'received:srv.osa': 0.16; 'string': 0.17; 'wrote:': 0.17; 'string,': 0.17; 'subject:Issue': 0.17; '>>>': 0.18; 'feb': 0.19; 'occurs': 0.22; 'pass': 0.25; 'header :In-Reply-To:1': 0.25; 'once.': 0.29; 'to:addr:python-list': 0.33; 'times.': 0.33; 'pm,': 0.35; 'subject:with': 0.36; 'two': 0.37; 'subject:: ': 0.38; 'received:10': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'header:Message-Id:1': 0.62; 'times': 0.63; 'reverse': 0.65 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=message-id:from:to:mime-version :content-transfer-encoding:content-type:subject:date:in-reply-to :references; s=smtpout; bh=oEXDuuc6/Mt1ydNHu61TXi9BOuY=; b=qagwX gONoNcM3UatvNJtWKosj/uPHCh28+L46ydxE2nX0ZjGgQ87qUDnIrQQpvfu/iUMd e3O/HAMdaFja6LpXw/eDqWzatq95QbSdB1Dnua6FTNtrHf33dyztbMuky83H+eV+ I/8dVnP1etVcqD4BqnAb/ZBAEtD8R3WeW12u8s= X-Sasl-Enc: cK83ClnrOHZGPkPycDYEdKWjclLe+xi+/+jbXg3An8ry 1360101916 From: marduk To: python-list@python.org MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="UTF-8" X-Mailer: MessagingEngine.com Webmail Interface - ajax-1ec1891e Subject: Re: Issue with my code Date: Tue, 05 Feb 2013 17:05:16 -0500 In-Reply-To: <282fd10c-2199-45b3-94aa-f57a38722442@h9g2000vbk.googlegroups.com> References: <2b0eb097-e575-4d6d-a509-f4c6bd58c934@googlegroups.com> <282fd10c-2199-45b3-94aa-f57a38722442@h9g2000vbk.googlegroups.com> 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: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360101919 news.xs4all.nl 6913 [2001:888:2000:d::a6]:52850 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38236 On Tue, Feb 5, 2013, at 04:37 PM, darnold wrote: > On Feb 5, 2:19=C2=A0pm, maiden129 wrote: > > How to reverse the two loops? > > >=20 > s=3Dinput("Enter a string, eg(4856w23874): ") >=20 > checkS=3D['0','1','2','3','4','5','6','7','8','9'] >=20 > for digit in checkS: > t =3D s.count(digit) > if t =3D=3D 0: > pass > elif t =3D=3D 1: > print(digit,"occurs 1 time.") > else: > print(digit, "occurs", t,"times.") >=20 >=20 > >>> > Enter a string, eg(4856w23874): 23493049weee2039412367 > 0 occurs 2 times. > 1 occurs 1 time. > 2 occurs 3 times. > 3 occurs 4 times. > 4 occurs 3 times. > 6 occurs 1 time. > 7 occurs 1 time. > 9 occurs 3 times. > >>> Although that implementation also scans the string 10 times (s.count()), which may not be as efficient (although it is happening in C, so perhaps not). A better solution involves only scanning the string once.