Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #38248

Re: Issue with my code

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.erje.net!us.feeder.erje.net!news2.arglkargh.de!nuzba.szn.dk!pnx.dk!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'elif': 0.04; 'subject:code': 0.07; 'occurrences': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'checks:': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'string': 0.17; 'wrote:': 0.17; 'string,': 0.17; 'subject:Issue': 0.17; 'jan': 0.18; 'trying': 0.21; 'occurs': 0.22; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'replace': 0.27; 'header:X-Complaints-To:1': 0.28; "i'm": 0.29; "skip:' 10": 0.30; 'checks': 0.30; 'to:addr:python- list': 0.33; 'code:': 0.33; 'hi,': 0.33; 'pm,': 0.35; 'received:org': 0.36; 'subject:with': 0.36; 'subject:: ': 0.38; 'easier': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'times': 0.63; 'here': 0.65; 'unnecessary': 0.65; 'counts': 0.81; "'0123456789'": 0.84; 'received:fios.verizon.net': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Issue with my code
Date Tue, 05 Feb 2013 19:06:03 -0500
References <2b0eb097-e575-4d6d-a509-f4c6bd58c934@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-173-75-251-66.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/17.0 Thunderbird/17.0
In-Reply-To <2b0eb097-e575-4d6d-a509-f4c6bd58c934@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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1399.1360109194.2939.python-list@python.org> (permalink)
Lines 50
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1360109195 news.xs4all.nl 6865 [2001:888:2000:d::a6]:48572
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:38248

Show key headers only | View raw


On 2/5/2013 1:38 PM, maiden129 wrote:
> 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)

Unnecessary conversion.

> checkS=['0','1','2','3','4','5','6','7','8','9']

checks = '0123456789' is much easier to type.
>
> for i in s:
>      if i in checkS:

Loop through checks, not s

>          t=s.count(i)
>          if t>1:
>              for k in range(1,t):
>                  s=s.remove(i)
>                  print(i, "occurs", t,"times.")
>
>          elif t==1:
>              print(i,"occurs 1 time.")
>      else: pass

Replace everything with

s = input("Enter a string of digits: ")
for d in '0123456789':
     c = s.count(d)
     if c:
         print("{} occurs {} time{}".format(d, c, '' if c == 1 else 's'))

Enter a string of digits: 12233344499
1 occurs 1 time
2 occurs 2 times
3 occurs 3 times
4 occurs 3 times
9 occurs 2 times

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Issue with my code maiden129 <sengokubasarafever@gmail.com> - 2013-02-05 10:38 -0800
  Re: Issue with my code maiden129 <sengokubasarafever@gmail.com> - 2013-02-05 10:43 -0800
  Re: Issue with my code marduk <marduk@python.net> - 2013-02-05 13:56 -0500
    Re: Issue with my code maiden129 <sengokubasarafever@gmail.com> - 2013-02-05 11:20 -0800
      Re: Issue with my code Dave Angel <davea@davea.name> - 2013-02-05 14:43 -0500
        Re: Issue with my code maiden129 <sengokubasarafever@gmail.com> - 2013-02-05 12:19 -0800
        Re: Issue with my code maiden129 <sengokubasarafever@gmail.com> - 2013-02-05 12:19 -0800
          Re: Issue with my code darnold <darnold992000@yahoo.com> - 2013-02-05 13:37 -0800
            Re: Issue with my code marduk <marduk@python.net> - 2013-02-05 17:05 -0500
              Re: Issue with my code darnold <darnold992000@yahoo.com> - 2013-02-05 14:25 -0800
    Re: Issue with my code maiden129 <sengokubasarafever@gmail.com> - 2013-02-05 11:20 -0800
  Re: Issue with my code MRAB <python@mrabarnett.plus.com> - 2013-02-05 19:06 +0000
  Re: Issue with my code Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-02-05 17:20 -0500
  Re: Issue with my code Terry Reedy <tjreedy@udel.edu> - 2013-02-05 19:06 -0500
  Re: Issue with my code rusi <rustompmody@gmail.com> - 2013-02-05 22:29 -0800
    Re: Issue with my code rusi <rustompmody@gmail.com> - 2013-02-05 22:32 -0800

csiph-web