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


Groups > comp.lang.python > #38217

Re: Issue with my code

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!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
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; 'example:': 0.03; 'elif': 0.04; 'attribute': 0.05; 'error:': 0.05; 'none,': 0.05; 'subject:code': 0.07; 'collections': 0.09; 'none.': 0.09; 'occurrences': 0.09; ':-)': 0.13; 'checks:': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'string:': 0.16; 'string': 0.17; 'wrote:': 0.17; "shouldn't": 0.17; 'string,': 0.17; 'subject:Issue': 0.17; 'changes': 0.20; 'trying': 0.21; 'occurs': 0.22; 'this:': 0.23; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'executing': 0.27; 'module.': 0.27; 'class': 0.29; "i'm": 0.29; 'to:addr:python-list': 0.33; 'code:': 0.33; 'hi,': 0.33; 'list': 0.35; 'add': 0.36; 'but': 0.36; 'wanted': 0.36; 'method': 0.36; 'subject:with': 0.36; 'itself': 0.37; 'keeps': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'list,': 0.39; 'received:192.168': 0.40; 'your': 0.60; 'most': 0.61; 'remove': 0.61; 'first': 0.61; 'time,': 0.62; 'show': 0.63; 'here': 0.65; 'header:Reply-To:1': 0.68; 'reply- to:no real name:2**0': 0.72; 'counts': 0.81; 'case?': 0.84; 'reply-to:addr:python.org': 0.84; "'remove'": 0.91
X-CM-Score 0.00
X-CNFS-Analysis v=2.0 cv=XeZXOvF5 c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=Tv4_tr9OjFAA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=8vuQrxt87CgA:10 a=0PABKfTFvorNYfjudEkA:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117
X-AUTH mrabarnett:2500
Date Tue, 05 Feb 2013 19:06:46 +0000
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130107 Thunderbird/17.0.2
MIME-Version 1.0
To python-list@python.org
Subject Re: Issue with my code
References <2b0eb097-e575-4d6d-a509-f4c6bd58c934@googlegroups.com>
In-Reply-To <2b0eb097-e575-4d6d-a509-f4c6bd58c934@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To python-list@python.org
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.1380.1360091206.2939.python-list@python.org> (permalink)
Lines 50
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1360091206 news.xs4all.nl 6989 [2001:888:2000:d::a6]:56022
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:38217

Show key headers only | View raw


On 2013-02-05 18:38, 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)
>
> checkS=['0','1','2','3','4','5','6','7','8','9']
>
> for i in s:
>      if i in checkS:
>          t=s.count(i)
>          if t>1:
>              for k in range(1,t):
>                  s=s.remove(i)

The 'remove' method changes the list itself and then returns None, so
after executing this line the first time, s will be None.

>                  print(i, "occurs", t,"times.")
>
>          elif t==1:
>              print(i,"occurs 1 time.")
>      else: pass
>
> but it keeps showing this error:
>
>   t=s.count(i)
> AttributeError: 'NoneType' object has no attribute 'count'
>
> I wanted to show like this:
>
> Example:
>
> Enter a string: 3233456
>
> 3 occurs 3
> 2 occurs 1
> 4 occurs 1
> 5 occurs 1
> 6 occurs 1
>
You shouldn't add or remove items from a collection, such as a list,
over which you're iterating. Is it even necessary in this case? No.

Have a look at the Counter class in the collections module. That'll let
you eliminate most of your code! :-)

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