spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

letter frequency from a string in a file using python


If you want the count of all the letters in the file use a collections.Counter dict:

from collections import Counter
c = Counter()
with open("input.txt") as f:
    for line in f:
        c.update(line.split(",")[0])
    print(c)
Counter({'e': 16, 'r': 12, 'd': 8, 'a': 6, 't': 6,
'n': 4, 'q': 4, 's': 4, 'u': 4, 'w': 4, 'i': 2,
'o': 2, 'p': 2})

To get total just multiply by the times it appears:

from collections import Counter
c = Counter()
with open("input.txt") as f:
    for line in f:
        word, year, count = line.split()
        c.update(word*int(count))

 print(c["a"] / float(sum(c.values())))

Categories : Python

Related to : letter frequency from a string in a file using python
Why is log used when calculating term frequency weight and IDF, inverse document frequency?
It is not necessarily the case that more the occurrence of a term in a document more is the relevance... the contribution of term frequency to document relevance is essentially a sub-linear function... hence the log to approximate this sub-linear function... the same is applicable for idf as well... a linear idf function may be boosting too much the document scores with high idf terms (which coul

Categories : Misc
Replace specific letter in string
This could be easily done like this: var string = "dynamic_fields[n][key]"; var replaced = string.replace(/[n]/,"[1]"); alert(replaced); Basically, this finds the character "n" that is surrounded with brackets, and replaces it with a "1" surrounded with brackets. Remeber to "escape" the brackets to make them literal. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Values,_var

Categories : Javascript
python dictionary creating keys on letters and values on frequency it appears
Simply pass the whole string to a collections.Counter() object and it'll count each character. It may be more efficient to do so line by line, so as not to require too much memory: from collections import Counter counts = Counter() with open('inputtextfilename') as infh: for line in infh: counts.update(line.strip()) The str.strip() call removes any whitespace (such as the newline

Categories : Python
Regex to validate string having only lower case, first char must be a letter
Why don't you just stick to your requirements ? first char must be a lowercase letter: [a-z] remaining characters must match: [a-z0-9_.] -> your regex: /^[a-z][a-z0-9_.]*$/

Categories : Ruby
Rotating the characters in a string with a specific group/list of letter and symbols?
Here's a cool way rotated = ''.join([mystring[i-offset] for i in range(len(mystring))]) It can overflow (get an out of bounds error) if the offset is too high, which you'll have to account for if relevant Explanation: we index into the string and subtract by offset, making use of the fact that negative indexing wraps in Python (though only once, hence the possible overflow). List comprehensi

Categories : Python
Recently Add
Overriding save method vs Model Method?
function encrypt file - input & output to same file in python
urllib.error.URLError:
python if statement fails to compute compound logic
referencing sphinx generated Python docs from YAML file
Subclassing datetime64
filter view from url parameter with an intermediate model?
How to stream twitter mentions with tweepy?
How to space out rows and columns in Python Tkinter
Python: Using BeautifulSoup to save content to CSV
Python - Take parts of a txt file and put them in another txt
Python socket send doesn't send the whole string
Python: prime test
How to import one day old logs
Appending filters to django models
Python: parsing .xls file failed with both xlrd and pandas
writing pandas dataframe columns to csv rows staggered
parse_args doesn't seem to understand json arguments
Django ORM, get data
Pygame, keep jumping and glitching through the "ground"
Is it possible to import a class defined inside a function?
How to handle a variable number of nested for loops?
Boolean expression with strings
Object duplicated when using processing.Manager?
What is the syntax of PBS job limit directive
python 3.x overwrite previous terminal line
Deploying django app. on heroku (gunicorn, wsgi issue)
How can I create an "ID" column in one dataframe based on the index of another dataframe?
PIL Module Error
pandas.read_html returns only one table
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.