Velocity Reviews > Issue with my code

# Issue with my code

marduk
Guest
Posts: n/a

 02-05-2013

On Tue, Feb 5, 2013, at 04:37 PM, darnold wrote:
> On Feb 5, 2:19Â*pm, maiden129 <(E-Mail Removed)> 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.

Dennis Lee Bieber
Guest
Posts: n/a

 02-05-2013
On Tue, 5 Feb 2013 10:38:55 -0800 (PST), maiden129
<(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) HTTP://wlfraed.home.netcom.com/

darnold
Guest
Posts: n/a

 02-05-2013
On Feb 5, 4:05*pm, marduk <(E-Mail Removed)> 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

Terry Reedy
Guest
Posts: n/a

 02-06-2013
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

rusi
Guest
Posts: n/a

 02-06-2013
On Feb 5, 11:38*pm, maiden129 <(E-Mail Removed)> 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]

rusi
Guest
Posts: n/a

 02-06-2013
> 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"

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Skybuck Flying Windows 64bit 0 04-07-2007 03:12 PM the pez lover Computer Support 1 02-05-2007 02:44 AM keithb ASP .Net 1 03-29-2006 01:00 AM joe.valentine@gmail.com Computer Support 8 02-06-2006 09:03 PM =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?= ASP .Net 4 02-11-2004 07:31 AM