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


Groups > comp.lang.python > #38213 > unrolled thread

Issue with my code

Started bymaiden129 <sengokubasarafever@gmail.com>
First post2013-02-05 10:38 -0800
Last post2013-02-05 22:32 -0800
Articles 16 — 8 participants

Back to article view | Back to comp.lang.python


Contents

  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

#38213 — Issue with my code

Frommaiden129 <sengokubasarafever@gmail.com>
Date2013-02-05 10:38 -0800
SubjectIssue with my code
Message-ID<2b0eb097-e575-4d6d-a509-f4c6bd58c934@googlegroups.com>
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)
                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

[toc] | [next] | [standalone]


#38214

Frommaiden129 <sengokubasarafever@gmail.com>
Date2013-02-05 10:43 -0800
Message-ID<b6d5a637-e2f9-40ad-9c71-1b7fe0015d17@googlegroups.com>
In reply to#38213
Also I’m using Python 3.2.3.

On Tuesday, February 5, 2013 1:38:55 PM UTC-5, 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)
> 
>                 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

[toc] | [prev] | [next] | [standalone]


#38216

Frommarduk <marduk@python.net>
Date2013-02-05 13:56 -0500
Message-ID<mailman.1379.1360090617.2939.python-list@python.org>
In reply to#38213

On Tue, Feb 5, 2013, at 01: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)
> 
> 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)
>                 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'

s=s.remove(i) does not return a new list but modifies the list  in
place.

So you probably just want

>>> s.remove(i)

Also, there are various inefficiencies in your code, but that is the
main issue with the AttributeError.

[toc] | [prev] | [next] | [standalone]


#38218

Frommaiden129 <sengokubasarafever@gmail.com>
Date2013-02-05 11:20 -0800
Message-ID<db762eb7-0901-4feb-bdf8-ac14aca4a711@googlegroups.com>
In reply to#38216
On Tuesday, February 5, 2013 1:56:55 PM UTC-5, marduk wrote:
> On Tue, Feb 5, 2013, at 01: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)
> 
> > 
> 
> > 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)
> 
> >                 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'
> 
> 
> 
> s=s.remove(i) does not return a new list but modifies the list  in
> 
> place.
> 
> 
> 
> So you probably just want
> 
> 
> 
> >>> s.remove(i)
> 
> 
> 
> Also, there are various inefficiencies in your code, but that is the
> 
> main issue with the AttributeError.

when I removed "s.remove(i), it starts to repeat the number of occurrences too 

many times like this:

2 occurs 3 times.
2 occurs 3 times.
3 occurs 3 times.
3 occurs 3 times.
2 occurs 3 times.
2 occurs 3 times.
5 occurs 1 time.
3 occurs 3 times.
3 occurs 3 times.
4 occurs 1 time.
3 occurs 3 times.
3 occurs 3 times.
1 occurs 1 time.
2 occurs 3 times.
2 occurs 3 times. 

How can I stop this?

[toc] | [prev] | [next] | [standalone]


#38222

FromDave Angel <davea@davea.name>
Date2013-02-05 14:43 -0500
Message-ID<mailman.1384.1360093449.2939.python-list@python.org>
In reply to#38218
On 02/05/2013 02:20 PM, maiden129 wrote:
> On Tuesday, February 5, 2013 1:56:55 PM UTC-5, marduk wrote:
>> On Tue, Feb 5, 2013, at 01:38 PM, maiden129 wrote:
>>
<Snipping double-spaced googlegroups trash>
>
> when I removed "s.remove(i), it starts to repeat the number of occurrences too
>
> many times like this:
>
> 2 occurs 3 times.
> 2 occurs 3 times.
> 3 occurs 3 times.
> 3 occurs 3 times.
> 2 occurs 3 times.
> 2 occurs 3 times.
> 5 occurs 1 time.
> 3 occurs 3 times.
> 3 occurs 3 times.
> 4 occurs 1 time.
> 3 occurs 3 times.
> 3 occurs 3 times.
> 1 occurs 1 time.
> 2 occurs 3 times.
> 2 occurs 3 times.
>
> How can I stop this?
>

As MRAB pointed out, don't delete items from a list you're iterating 
over.  It can make the iterator go nuts.  He suggests the collections 
module.

But if you want to do it by hand, one approach is to reverse the two 
loops.  Iterate over the characters in CheckS list, examining the entire 
s list for each one and figuring out how many times the character occurs.

Another approach is to build a dict, or a defaultdict, to keep counts 
for each of the characters in CheckS.

-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#38226

Frommaiden129 <sengokubasarafever@gmail.com>
Date2013-02-05 12:19 -0800
Message-ID<fca6e15e-b386-4d6d-954f-5647453c3228@googlegroups.com>
In reply to#38222
How to reverse the two loops?

On Tuesday, February 5, 2013 2:43:47 PM UTC-5, Dave Angel wrote:
> On 02/05/2013 02:20 PM, maiden129 wrote:
> 
> > On Tuesday, February 5, 2013 1:56:55 PM UTC-5, marduk wrote:
> 
> >> On Tue, Feb 5, 2013, at 01:38 PM, maiden129 wrote:
> 
> >>
> 
> <Snipping double-spaced googlegroups trash>
> 
> >
> 
> > when I removed "s.remove(i), it starts to repeat the number of occurrences too
> 
> >
> 
> > many times like this:
> 
> >
> 
> > 2 occurs 3 times.
> 
> > 2 occurs 3 times.
> 
> > 3 occurs 3 times.
> 
> > 3 occurs 3 times.
> 
> > 2 occurs 3 times.
> 
> > 2 occurs 3 times.
> 
> > 5 occurs 1 time.
> 
> > 3 occurs 3 times.
> 
> > 3 occurs 3 times.
> 
> > 4 occurs 1 time.
> 
> > 3 occurs 3 times.
> 
> > 3 occurs 3 times.
> 
> > 1 occurs 1 time.
> 
> > 2 occurs 3 times.
> 
> > 2 occurs 3 times.
> 
> >
> 
> > How can I stop this?
> 
> >
> 
> 
> 
> As MRAB pointed out, don't delete items from a list you're iterating 
> 
> over.  It can make the iterator go nuts.  He suggests the collections 
> 
> module.
> 
> 
> 
> But if you want to do it by hand, one approach is to reverse the two 
> 
> loops.  Iterate over the characters in CheckS list, examining the entire 
> 
> s list for each one and figuring out how many times the character occurs.
> 
> 
> 
> Another approach is to build a dict, or a defaultdict, to keep counts 
> 
> for each of the characters in CheckS.
> 
> 
> 
> -- 
> 
> DaveA

[toc] | [prev] | [next] | [standalone]


#38227

Frommaiden129 <sengokubasarafever@gmail.com>
Date2013-02-05 12:19 -0800
Message-ID<mailman.1386.1360095557.2939.python-list@python.org>
In reply to#38222
How to reverse the two loops?

On Tuesday, February 5, 2013 2:43:47 PM UTC-5, Dave Angel wrote:
> On 02/05/2013 02:20 PM, maiden129 wrote:
> 
> > On Tuesday, February 5, 2013 1:56:55 PM UTC-5, marduk wrote:
> 
> >> On Tue, Feb 5, 2013, at 01:38 PM, maiden129 wrote:
> 
> >>
> 
> <Snipping double-spaced googlegroups trash>
> 
> >
> 
> > when I removed "s.remove(i), it starts to repeat the number of occurrences too
> 
> >
> 
> > many times like this:
> 
> >
> 
> > 2 occurs 3 times.
> 
> > 2 occurs 3 times.
> 
> > 3 occurs 3 times.
> 
> > 3 occurs 3 times.
> 
> > 2 occurs 3 times.
> 
> > 2 occurs 3 times.
> 
> > 5 occurs 1 time.
> 
> > 3 occurs 3 times.
> 
> > 3 occurs 3 times.
> 
> > 4 occurs 1 time.
> 
> > 3 occurs 3 times.
> 
> > 3 occurs 3 times.
> 
> > 1 occurs 1 time.
> 
> > 2 occurs 3 times.
> 
> > 2 occurs 3 times.
> 
> >
> 
> > How can I stop this?
> 
> >
> 
> 
> 
> As MRAB pointed out, don't delete items from a list you're iterating 
> 
> over.  It can make the iterator go nuts.  He suggests the collections 
> 
> module.
> 
> 
> 
> But if you want to do it by hand, one approach is to reverse the two 
> 
> loops.  Iterate over the characters in CheckS list, examining the entire 
> 
> s list for each one and figuring out how many times the character occurs.
> 
> 
> 
> Another approach is to build a dict, or a defaultdict, to keep counts 
> 
> for each of the characters in CheckS.
> 
> 
> 
> -- 
> 
> DaveA

[toc] | [prev] | [next] | [standalone]


#38231

Fromdarnold <darnold992000@yahoo.com>
Date2013-02-05 13:37 -0800
Message-ID<282fd10c-2199-45b3-94aa-f57a38722442@h9g2000vbk.googlegroups.com>
In reply to#38227
On Feb 5, 2:19 pm, maiden129 <sengokubasarafe...@gmail.com> wrote:
> How to reverse the two loops?
>

s=input("Enter a string, eg(4856w23874): ")

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

for digit in checkS:
    t = s.count(digit)
    if t == 0:
        pass
    elif t == 1:
        print(digit,"occurs 1 time.")
    else:
        print(digit, "occurs", t,"times.")


>>>
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.
>>>

HTH,
Don

[toc] | [prev] | [next] | [standalone]


#38236

Frommarduk <marduk@python.net>
Date2013-02-05 17:05 -0500
Message-ID<mailman.1392.1360101919.2939.python-list@python.org>
In reply to#38231

On Tue, Feb 5, 2013, at 04:37 PM, darnold wrote:
> On Feb 5, 2:19 pm, maiden129 <sengokubasarafe...@gmail.com> wrote:
> > How to reverse the two loops?
> >
> 
> s=input("Enter a string, eg(4856w23874): ")
> 
> checkS=['0','1','2','3','4','5','6','7','8','9']
> 
> for digit in checkS:
>     t = s.count(digit)
>     if t == 0:
>         pass
>     elif t == 1:
>         print(digit,"occurs 1 time.")
>     else:
>         print(digit, "occurs", t,"times.")
> 
> 
> >>>
> 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.

[toc] | [prev] | [next] | [standalone]


#38239

Fromdarnold <darnold992000@yahoo.com>
Date2013-02-05 14:25 -0800
Message-ID<b2c50176-7133-43b5-a1a8-9eeb883449e6@e18g2000vbv.googlegroups.com>
In reply to#38236
On Feb 5, 4:05 pm, marduk <mar...@python.net> wrote:
>
> 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.

agreed. i was specifically showing how to reverse the loop.
using the much-better-suited Counter class:


from collections import Counter

s=input("Enter a string, eg(4856w23874): ")

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

cnt = Counter()

for char in s:
    cnt[char] += 1

for char, tally in sorted(cnt.items()):
    if char in checkS and tally > 0:
        if tally == 1:
            print(char,"occurs 1 time.")
        else:
            print(char, "occurs", tally,"times.")

>>>
Enter a string, eg(4856w23874): 192398209asdfbc12903348955
0 occurs 2 times.
1 occurs 2 times.
2 occurs 3 times.
3 occurs 3 times.
4 occurs 1 time.
5 occurs 2 times.
8 occurs 2 times.
9 occurs 5 times.
>>>

HTH,
Don

[toc] | [prev] | [next] | [standalone]


#38234

Frommaiden129 <sengokubasarafever@gmail.com>
Date2013-02-05 11:20 -0800
Message-ID<mailman.1390.1360101357.2939.python-list@python.org>
In reply to#38216
On Tuesday, February 5, 2013 1:56:55 PM UTC-5, marduk wrote:
> On Tue, Feb 5, 2013, at 01: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)
> 
> > 
> 
> > 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)
> 
> >                 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'
> 
> 
> 
> s=s.remove(i) does not return a new list but modifies the list  in
> 
> place.
> 
> 
> 
> So you probably just want
> 
> 
> 
> >>> s.remove(i)
> 
> 
> 
> Also, there are various inefficiencies in your code, but that is the
> 
> main issue with the AttributeError.

when I removed "s.remove(i), it starts to repeat the number of occurrences too 

many times like this:

2 occurs 3 times.
2 occurs 3 times.
3 occurs 3 times.
3 occurs 3 times.
2 occurs 3 times.
2 occurs 3 times.
5 occurs 1 time.
3 occurs 3 times.
3 occurs 3 times.
4 occurs 1 time.
3 occurs 3 times.
3 occurs 3 times.
1 occurs 1 time.
2 occurs 3 times.
2 occurs 3 times. 

How can I stop this?

[toc] | [prev] | [next] | [standalone]


#38217

FromMRAB <python@mrabarnett.plus.com>
Date2013-02-05 19:06 +0000
Message-ID<mailman.1380.1360091206.2939.python-list@python.org>
In reply to#38213
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! :-)

[toc] | [prev] | [next] | [standalone]


#38238

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2013-02-05 17:20 -0500
Message-ID<mailman.1394.1360102819.2939.python-list@python.org>
In reply to#38213
On Tue, 5 Feb 2013 10:38:55 -0800 (PST), maiden129
<sengokubasarafever@gmail.com> 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/

[toc] | [prev] | [next] | [standalone]


#38248

FromTerry Reedy <tjreedy@udel.edu>
Date2013-02-05 19:06 -0500
Message-ID<mailman.1399.1360109194.2939.python-list@python.org>
In reply to#38213
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

[toc] | [prev] | [next] | [standalone]


#38258

Fromrusi <rustompmody@gmail.com>
Date2013-02-05 22:29 -0800
Message-ID<6b338868-ed56-4bd6-ba48-8df1ed62c7a4@j6g2000yqj.googlegroups.com>
In reply to#38213
On Feb 5, 11:38 pm, maiden129 <sengokubasarafe...@gmail.com> 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)
>                 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

Pythons 2.7 and later have dictionary comprehensions. So you can do
this:


>>> {item: s.count(item) for item in set(s)}
{'a': 1, 'b': 1, '1': 2, '3': 1, '2': 2, '4': 1}

Which gives counts for all letters. To filter out the digit-counts
only:

>>> digits="0123456789"
>>> {item: s.count(item) for item in set(s) if item in dig}
{'1': 2, '3': 1, '2': 2, '4': 1}

You can then print out the values in d in any which way you want.
[Starting with printing is usually a bad idea]

[toc] | [prev] | [next] | [standalone]


#38259

Fromrusi <rustompmody@gmail.com>
Date2013-02-05 22:32 -0800
Message-ID<e52ed9ed-a3e1-47ba-9f0d-b72e18d1c286@j6g2000yqj.googlegroups.com>
In reply to#38258
> Pythons 2.7 and later have dictionary comprehensions. So you can do
> this:
>
> >>> {item: s.count(item) for item in set(s)}
>
> {'a': 1, 'b': 1, '1': 2, '3': 1, '2': 2, '4': 1}
>
> Which gives counts for all letters. To filter out the digit-counts
> only:
>
> >>> digits="0123456789"
> >>> {item: s.count(item) for item in set(s) if item in dig}
>
> {'1': 2, '3': 1, '2': 2, '4': 1}
>
> You can then print out the values in d in any which way you want.
> [Starting with printing is usually a bad idea]

Sorry cut-paste slip-up.
1. use dig or digits (or none, just inline the "0123456789")
2. I assumed
>>> s = "12ab3412"

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web