| | |
Letter frequency function using lists
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
A simple function to return the number of occurances of a letter in a text. An easier way to implement this would be by using a dictionary 
P.S sorry if there already is a code snipped like that somewhere.

P.S sorry if there already is a code snipped like that somewhere.
def letterFrequency(): newString = raw_input( "Type the string here: " ) #Convert to lowercase mod_string = newString.lower() #Initialize lists to store the new data in data = [] new_data = [] count = 0 #Check only for characters in the given string for s in 'abcdefghijklmnopqrstuvwxyz': count = 0 for char in mod_string: if s == char: count += 1 data = [ s, count ] new_data += [ data ] return new_data print letterFrequency()
0
•
•
•
•
yeah you're right a quick fix would be to add an if statement in line 19, like
thanks
Python Syntax (Toggle Plain Text)
if count > 0: data = [ s, count ] new_data += [ data ]
thanks

Similar Threads
- Trying to put the value of a variable into a function parameter (JavaScript / DHTML / AJAX)
- java unscrambler (Java)
- Code Breaker Project (C++)
- A Function (C++)
- traversing backwards thru double linked list (C++)
- newbie in need of help for hangman game (Python)
- lists and vectors (C)
- Cryptography Fun (Python)
- in need of a function (Java)
| Thread Tools | Search this Thread |
abrupt ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog data decimals dictionaries dictionary directory 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 tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable ventrilo wikipedia windows write wxpython xlib



