Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #38239
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2013-02-05 14:25 -0800 |
| References | (2 earlier) <db762eb7-0901-4feb-bdf8-ac14aca4a711@googlegroups.com> <mailman.1384.1360093449.2939.python-list@python.org> <mailman.1386.1360095557.2939.python-list@python.org> <282fd10c-2199-45b3-94aa-f57a38722442@h9g2000vbk.googlegroups.com> <mailman.1392.1360101919.2939.python-list@python.org> |
| Message-ID | <b2c50176-7133-43b5-a1a8-9eeb883449e6@e18g2000vbv.googlegroups.com> (permalink) |
| Subject | Re: Issue with my code |
| From | darnold <darnold992000@yahoo.com> |
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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