| | |
more efficient print method
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
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 ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog data decimals dictionaries dictionary drive dynamic error examples exe file float format function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists logging loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule script scrolledtext sqlite statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable ventrilo wikipedia windows write wxpython xlib





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