| | |
more efficient print method
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 43
Reputation:
Solved Threads: 0
I have an array of numbers and want to print out how many of each number there are in the array. My method works but I was wondering what a more efficient way of doing this would be. I figure a loop would work but I haven't figured out how to do it with this:
I did find this snippet which is fast in counting but doesn't print out like I want it to:
Python Syntax (Toggle Plain Text)
s='1234444432187667890000568984' print 'I found %i:' % s.count('0'),0 print 'I found %i:' % s.count('1'),1 print 'I found %i:' % s.count('2'),2 print 'I found %i:' % s.count('3'),3 print 'I found %i:' % s.count('4'),4 print 'I found %i:' % s.count('5'),5 print 'I found %i:' % s.count('6'),6 print 'I found %i:' % s.count('7'),7 print 'I found %i:' % s.count('8'),8 print 'I found %i:' % s.count('9'),9
I did find this snippet which is fast in counting but doesn't print out like I want it to:
Python Syntax (Toggle Plain Text)
s='1234444432187667890000568984' ans=dict((i,s.count(i)) for i in set(s)) print ans
1
#2 Oct 21st, 2009
python Syntax (Toggle Plain Text)
s='1234444432187667890000568984' print("\n".join(("I found %d: %d" % (s.count(str(i)), i)) for i in range(10)))
![]() |
Similar Threads
- Trying to call method in child class (Java)
- print web page (JavaScript / DHTML / AJAX)
- print text in multiple lines in picturebox in vb6 (Visual Basic 4 / 5 / 6)
- Help on Assignment (Java)
- How do i print asp page on client side "only the contents" (ASP)
- Printing using Web Control Print button VB.NET (JavaScript / DHTML / AJAX)
- Printing using Web Control Print button. (ASP.NET)
- Dynamically Print Out Structure Contents (C++)
Other Threads in the Python Forum
- Previous Thread: How to modify output result by using Python?
- Next Thread: Panda or Pygame - just curious
| Thread Tools | Search this Thread |
abrupt accessdenied anti apache application approximation argv array beginner book builtin calculator change converter countpasswordentry curved dan08 dictionaries dictionary dynamic edit enter examples file float format function gui homework import inches input java keyboard lapse launcher library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric output parameters parsing path phonebook plugin port prime programming projects py2exe pygame pyopengl pysimplewizard python random recursion redirect remote reverse scrolledtext session simple software sprite statictext string strings syntax table terminal text textarea thread threading time tlapse trick tuple tutorial twoup ubuntu unicode unit urllib urllib2 variable wordgame wxpython





very impressive and exactly what I was looking for. Thanks! 