| | |
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 |
alarm app beginner cipher cmd cx-freeze data decimals development dictionary directory dynamic error examples feet file float format function generator getvalue gui halp homework http images import input ip itunes java keycontrol leftmouse line linux list lists logging loop maintain maze millimeter module mouse mysqldb number numbers output parsing path port prime programming projects push py2exe pygame pyglet pymailer pyqt python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation split sqlite ssh string strings sudokusolver table terminal text thread threading time tlapse tuple tutorial ubuntu unicode url urllib urllib2 variable variables ventrilo verify vigenere web webservice wikipedia wx.wizard wxpython xlwt





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