How to perform letter frequency? - python

This problem requires me to find the frequency analysis of a .txt file.
This is my code so far:
This finds the frequency of the words, but how would I get the frequency of the actual letters?
f = open('cipher.txt', 'r')
word_count = []
for c in f:
word_count.append(c)
word_count.sort()
decoding = {}
for i in word_count:
decoding[i] = word_count.count(i)
for n in decoding:
print(decoding)
This outputs (as a short example, since the txt file is pretty long):
{'\n': 12, 'vlvf zev jvg jrgs gvzef\n': 1, 'z uvfgriv sbhfv bu wboof!\n': 1, "gsv yrewf zoo nbhea zaw urfsvf'\n": 1, 'xbhow ube gsv avj bjave yv\n': 1, ' gsv fcerat rf czffrat -\n': 1, 'viva gsrf tezff shg\n': 1, 'bph ab sbfbnrxsr (azeebj ebzw gb gsv wvvc abegs)\n': 1, 'cbfg rafrwv gsv shg.\n': 1, 'fb gszg lvze -- gsv fvxbaw lvze bu tvaebph [1689] -- r szw fhwwvaol gzpva\n': 1, 'fb r czgxsvw hc nl gebhfvef, chg avj xbewf ra nl fgezj szg, zaw\n': 1, 'fcrergf bu gsv ebzw yvxpbavw nv, zaw r xbhow abg xbaxvagezgv ba zalgsrat.\n': 1, 'fgbbw zg gsv xebffebzwf bu czegrat, r jvcg tbbwylv.\n': 1,
It gives me the words, but how would I get the letters, such as how many "a"'s there are, or how many "b"'s there are?

Counter is quite a useful class native to Python, which can be used to solve your problem elegantly.
# count the letter freqency
from collections import Counter
with open('cipher.txt', 'r') as f:
s = f.read()
c = Counter(s) # the type of c is collection.Counter
# if you want dict as your output type
decoding = dict(c)
print(decoding)
If you put "every parting from you is like a little eternity" to your cipher.txt, you'll get the following result with the code above:
{'e': 6, 'v': 1, 'r': 4, 'y': 3, ' ': 8, 'p': 1, 'a': 2, 't': 5, 'i': 5, 'n': 2, 'g': 1, 'f': 1, 'o': 2, 'm': 1, 'u': 1, 's': 1, 'l': 3, 'k': 1}
However, if you want to implement the counting by yourself, here's a possible solution, providing the same result as using Counter.
# count the letter freqency, manually, without using collections.Counter
with open('cipher.txt', 'r') as f:
s = f.read()
decoding = {}
for c in s:
if c in decoding:
decoding[c] += 1
else:
decoding[c] = 1
print(decoding)

You can use a Counter from the collections standard library, it'll generate a dictionary of results:
from collections import Counter
s = """
This problem requires me to find the frequency analysis of a .txt file.
This is my code so far: This finds the frequency of the words, but how would I get the frequency of the actual letters?"""
c = Counter(s)
print(c.most_common(5))
This will print:
[(' ', 35), ('e', 20), ('t', 13), ('s', 11), ('o', 10)]
EDIT: Without using a Counter, we can use a dictionary and keep incrementing the count:
c = {}
for character in s:
try:
c[character] += 1
except KeyError:
c[character] = 1
print(c)
This will print:
{'\n': 4, 'T': 3, 'h': 9, 'i': 9, 's': 11, ' ': 35, 'p': 1, 'r': 9, 'o': 10, 'b': 2, 'l': 6, 'e': 20, 'm': 3, 'q': 4, 'u': 7, 't': 13, 'f': 10, 'n': 6, 'd': 5, 'c': 5, 'y': 5, 'a': 6, '.': 2, 'x': 1, ':': 1, 'w': 3, ',': 1, 'I': 1, 'g': 1, '?': 1}

Related

Python, how to find patterns of different length and to sum the number of match

I have a list like that:hg = [['A1'], ['A1b'], ['A1b1a1a2a1a~'], ['BT'], ['CF'], ['CT'], ['F'], ['GHIJK'], ['I'], ['I1a2a1a1d2a1a~'], ['I2'], ['I2~'], ['I2a'], ['I2a1'], ['I2a1a'], ['I2a1a2'], ['I2a1a2~'], ['IJ'], ['IJK'], ['L1a2']]
For example, if we look at :['A1'] ['A1b'] ['A1b1a1a2a1a~']
I want to count how many time the pattern 'A1','A1b' and 'A1b1a1a2a1a~' occurs.
Basically, A1 appears 3 times (A1 itself, A1 in A1b and A1 in A1b1a1a2a1a) and A1b two times (A1b itself and A1b in A1b1a1a2a1a) and A1b1a1a2a1a one time. Obviously, I want to do that for the entire list.
However, if in the list we have for example E1b1a1, I don't want to count a match of A1 in E1b1a1.
So what I did is:
dic_test = {}
for i in hg:
for j in hg:
if ''.join(i) in ''.join(j):
if ''.join(i) not in dic_test.keys():
dic_test[''.join(i)]=1
else:
dic_test[''.join(i)]+=1
print (dic_test)
output:{'A1': 3, 'A1b': 2, 'A1b1a1a2a1a~': 1, 'BT': 1, 'CF': 1, 'CT': 1, 'F': 2, 'GHIJK': 1, 'I': 12, 'I1a2a1a1d2a1a~': 1, 'I2': 7, 'I2~': 1, 'I2a': 5, 'I2a1': 4, 'I2a1a': 3, 'I2a1a2': 2, 'I2a1a2~': 1, 'IJ': 3, 'IJK': 2, 'L1a2': 1}
However, as explained above, there is one issue. For example, F should be equal at one and not 2. The reason is because with the code above, I look for F anywhere in the list. But I don't know how to correct that!
There is a second thing that I don't know how to do:
Based on the output:
{'A1': 3, 'A1b': 2, 'A1b1a1a2a1a~': 1, 'BT': 1, 'CF': 1, 'CT': 1, 'F': 2, 'GHIJK': 1, 'I': 12, 'I1a2a1a1d2a1a~': 1, 'I2': 7, 'I2~': 1, 'I2a': 5, 'I2a1': 4, 'I2a1a': 3, 'I2a1a2': 2, 'I2a1a2~': 1, 'IJ': 3, 'IJK': 2, 'L1a2': 1}
I would like to sum the values of the dic based on shared pattern:
example of the desired output{A1b1a1a2a1a~: 6, 'BT': 1,'CF': 1, 'CT': 1, 'F': 1, 'GHIJK': 1, 'I1a2a1a1d2a1a~': 13, I2a1a2:35, 'IJK': 5, 'IJK': 5}:
For example, A1b1a1a2a1a = 6 it's because it is made by A1 which has a value of 3, A1b with a value of 2 and the value of A1b1a1a2a1a equal at 1.
I don't know how to do that.
Any helps will be much appreciated!
Thanks
You count 'F' twice because you are iterating over the product of hg and hg so that the condition if ''.join(i) in ''.join(j) happens twice for 'F'. I solved that by checking the indexes.
You mentioned in the comment that the pattern should be at the beginning of the string so in doesn't work here. You can use .startswith() for that.
I first created a dictionary from the items but sorted(That's important for your second question about summing the values). They all start with the value of 1. Then I iterated over the the items, increased the value only if they are not in the same position.
For the second part of your question, because they are sorted, only the previous items can be at the beginning of the next items. So I got the pairs with .popitem() which hands the last pair (in Python 3.7 and above) and check its previous ones until the dictionary is empty.
hg = [['A1'], ['A1b'], ['A1b1a1a2a1a~'], ['BT'], ['CF'], ['CT'], ['F'], ['GHIJK'], ['I'], ['I1a2a1a1d2a1a~'], ['I2'], ['I2~'], ['I2a'], ['I2a1'], ['I2a1a'], ['I2a1a2'], ['I2a1a2~'], ['IJ'], ['IJK'], ['L1a2']]
# create a sorted dicitonary of all items each with the value of 1.
d = dict.fromkeys((item[0] for item in sorted(hg)), 1)
for idx1, (k, v) in enumerate(d.items()):
for idx2, item in enumerate(hg):
if idx1 != idx2 and item[0].startswith(k):
d[k] += 1
print(d)
print("-----------------------------------")
# last pair in `d`
k, v = d.popitem()
result = {k: v}
while d:
# pop last pair in `d`
k1, v1 = d.popitem()
# get last pair in `result`
k2, v2 = next(reversed(result.items()))
if k2.startswith(k1):
result[k2] += v1
else:
result[k1] = v1
print({k: result[k] for k in reversed(result)})
output:
{'A1': 3, 'A1b': 2, 'A1b1a1a2a1a~': 1, 'BT': 1, 'CF': 1, 'CT': 1, 'F': 1, 'GHIJK': 1, 'I': 11, 'I1a2a1a1d2a1a~': 1, 'I2': 7, 'I2a': 6, 'I2a1': 5, 'I2a1a': 4, 'I2a1a2': 3, 'I2a1a2~': 2, 'I2~': 2, 'IJ': 2, 'IJK': 1, 'L1a2': 1}
-----------------------------------
{'A1b1a1a2a1a~': 6, 'BT': 1, 'CF': 1, 'CT': 1, 'F': 1, 'GHIJK': 1, 'I1a2a1a1d2a1a~': 12, 'I2a1a2~': 27, 'I2~': 2, 'IJK': 3, 'L1a2': 1}
I think you made a mistake for your expected result and it should be like this, but let me know if mine is wrong.
#S.B helped me to better understand what I wanted to do, so I did some modifications to the second part of the script.
I converted the dictionary "d" (re-named "hg_d") into a list of list:
hg_d_to_list = list(map(list, hg_d.items()))
Then, I created a dictionary where the keys are the words and the values the list of the words that matches with startswith() like:
nested_HGs = defaultdict(list)
for i in range(len(hg_d_to_list)):
for j in range(i+1,len(hg_d_to_list)):
if hg_d_to_list[j][0].startswith(hg_d_to_list[i][0]):
nested_HGs[hg_d_to_list[j][0]].append(hg_d_to_list[i][0])
nested_HGs defaultdict(<class 'list'>, {'A1b': ['A1'], 'A1b1a1a2a1a': ['A1', 'A1b'], 'I1a2a1a1d2a1a~': ['I'], 'I2': ['I'], 'I2a': ['I', 'I2'], 'I2a1': ['I', 'I2', 'I2a'], 'I2a1a': ['I', 'I2', 'I2a', 'I2a1'], 'I2a1a2': ['I', 'I2', 'I2a', 'I2a1', 'I2a1a'], 'I2a1a2~': ['I', 'I2', 'I2a', 'I2a1', 'I2a1a', 'I2a1a2'], 'I2~': ['I', 'I2'], 'IJ': ['I'], 'IJK': ['I', 'IJ']})
Then, I sum each key and the value(s) associated to the dictionary "nested_HGs" based on the values of the dictionary "hg_d" like:
HGs_score = {}
for key,val in hg_d.items():
for key2,val2 in nested_HGs.items():
if key in val2 or key in key2:
if key2 not in HGs_score.keys():
HGs_score[key2]=val
else:
HGs_score[key2]+=val
HGs_score {'A1b': 5, 'A1b1a1a2a1a': 6, 'I1a2a1a1d2a1a~': 12, 'I2': 18, 'I2a': 24, 'I2a1': 29, 'I2a1a': 33, 'I2a1a2': 36, 'I2a1a2~': 38, 'I2~': 20, 'IJ': 13, 'IJK': 14}
Here, I realized that I don't care about the key with a value = at 1.
To finish, I get the key of the dictionary that has the highest value :
final_HG_classification = max(HGs_score, key=HGs_score.get)
final_HG_classification=I2a1a2~
It looks like it's working! Any suggestions or improvements are more than welcome.
Thanks in advance.

count letters of a file and create a histogram

I'm looking for some help with an issue I'm facing.
I'm trying to read a text file, count the number of times each letter occurs in the file using a dictionary.
Uppercase letters are turned into lowercase letters and only a-z in English are counted. Then display a star histogram like below from the counts and print a count of the total amount of letters.
I had the first part working, count the number of times each letter occurs in the file, until I added in my histogram code.
I'm not getting an error but the Terminal just displays this when ran:
{'d': 1}
My current code is:
def LetterCount(file_path):
file_path = file_path.lower().translate(file_path)
file_path = file_path.translate(string.punctuation)
file_path = file_path.strip(string.punctuation + string.whitespace)
list1=list(file_path)
lcDict= {}
with open(file_path,'r') as f:
for l in list1:
if l.isalpha():
if l in lcDict:
lcDict[l] +=1
else:
lcDict[l]= 1
return lcDict
file_path = '/myfolder/text.txt'
if __name__ == "__main__":
print(LetterCount(file_path))
def histogram(file_path):
sumValues = LetterCount(file_path)
padding = max(len(sumValues), len('Element'))
padding1 = max(len(str(max(sumValues))), len('Value'))
print("\nCreating a histogram from values: ")
print("%s %10s %10s" %("Element", "Value", "Histogram"))
for i,n in enumerate(sumValues, start=1):
('{0} {1} {2}'.format(
str(i).ljust(padding),
str(i).rjust(padding1),
'*'*n))
print(histogram(file_path)
What I'm trying to achieve from the histogram is this
a | *****
b | ***
c | ******
d | ****
e | *******
f | **
h | *****
...
z | *
I'd be really grateful for any help
because I don't have your file and cannot reproduce your specific example, I would answer the 2 questions apart.
First, in order to create an histogram as dictionary for your file (represented as list of strings) follow this part of code:
list_of_sentences = ["this is my first code in python", "it's rainy today", "thanks"]
m_dict = {}
for sentence in list_of_sentences:
for letter in sentence:
if letter.isalpha():
if letter in m_dict.keys():
m_dict[letter]+= 1
else:
m_dict[letter] =1
print(m_dict)
output:
{'t': 6, 'h': 3, 'i': 6, 's': 5, 'm': 1, 'y': 4, 'f': 1, 'r': 2, 'c': 1, 'o': 3, 'd': 2, 'e': 1, 'n': 4, 'p': 1, 'a': 3, 'k': 1}
The approach above will iterate over the letters in your file and count them, if you want to iterate over a to z, that for big files would be much efficient (moreover, it will print also letters that don't exist in your file), you better use this:
for code in range(ord('a'), ord('z') + 1):
m_dict[chr(code)] = ''.join(list_of_sentences).count(chr(code))
output:
{'t': 6, 'h': 3, 'i': 6, 's': 5, 'm': 1, 'y': 4, 'f': 1, 'r': 2, 'c': 1, 'o': 3, 'd': 2, 'e': 1, 'n': 4, 'p': 1, 'a': 3, 'k': 1, 'b': 0, 'g': 0, 'j': 0, 'l': 0, 'q': 0, 'u': 0, 'v': 0, 'w': 0, 'x': 0, 'z': 0}
Now when we have the histogram in our hands (let's continue with the first one), let's face the second part of formatting it as you want to:
def print_as_histogram(m_dict):
for letter in sorted(m_dict.keys()):
print(f'{letter} | {"*"*m_dict[letter]}')
print_as_histogram(m_dict)
output:
a | ***
c | *
d | **
e | *
f | *
h | ***
i | ******
k | *
m | *
n | ****
o | ***
p | *
r | **
s | *****
t | ******
y | ****
Sorted the letters, because it looks better in my opinion
You can use some standard libraries to make your life a bit easier!
import collections
import re
# Open the file
with open("./file.txt", 'r') as f:
txt = f.read()
# Find all the alphabetic characters
letters = re.findall("[a-zA-Z]", txt)
# Count them
counts = collections.Counter(letters)
# Print the star histogram
for i in 'abcdefghijklmnopqrstuvwxyz':
if i in counts:
print(f"{i} | {'*' * counts[i]}")
else: print(f"{i} | ")

How do i work on Checksum of Singapore Car License Plate with Python

I have researched and searched internet on the checksum of Singapore Car License Plate. For the license plate of SBA 1234, I need to convert all the digits excluding the S to numbers. A being 1, B being 2, and so on. SBA 1234 is in a string in a text format. How do i convert B and A to numbers for the calculation for the checksum while making sure that the value B and A do not change. The conversion of B and A to numbers is only for the calculation.
How do i do the conversion for this with Python. Please help out. Thank you.
There are multiple ways to create a dictionary with values A thru Z representing values 1 thru 26. One of the simple way to do it will be:
value = dict(zip("ABCDEFGHIJKLMNOPQRSTUVWXYZ", range(1,27)))
An alternate way to it would be using the ord() function.
ord('A') is 65. You can create a dictionary with values A thru Z representing values 1 thru 26. To do that, you can use simple code like this.
atoz = {chr(i): i - 64 for i in range(ord("A"), ord("A") + 26)}
This will provide an output with a dictionary
{'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5, 'F': 6, 'G': 7, 'H': 8, 'I': 9, 'J': 10, 'K': 11, 'L': 12, 'M': 13, 'N': 14, 'O': 15, 'P': 16, 'Q': 17, 'R': 18, 'S': 19, 'T': 20, 'U': 21, 'V': 22, 'W': 23, 'X': 24, 'Y': 25, 'Z': 26}
You can search for the char in the dictionary to get 1 thru 26.
Alternate, you can directly use ord(x) - 64 to get a value of the alphabet. If x is A, you will get 1. Similarly, if x is Z, the value will be 26.
So you can write the code directly to calculate the value of the Singapore Number Plate as:
snp = 'SBA 1234'
then you can get a value of
snp_num = [ord(snp[1]) - 64,ord(snp[2]) - 64, int(snp[4]), int(snp[5]), int(snp[6])]
This will result in
[2, 1, 1, 2, 3]
I hope one of these options will work for you. Then use the checksum function to do your calculation. I hope this is what you are looking for.

Indexing for a changing number python

I'm trying to figure out the index for this program. I want it to print a number for each letter entered in the input, for example the string "Jon" would be:
"10 15 14"
but I keep getting an error with the for loop I created with the indexes. If anyone has any thoughts on how to fix this it would be great help!
a = 1
b = 2
c = 3
d = 4
e = 5
f = 6
g = 7
h = 8
i = 9
j = 10
k = 11
l = 12
m = 13
n = 14
o = 15
p = 16
q = 17
r = 18
s = 19
t = 20
u = 21
v = 22
w = 23
x = 24
y = 25
z = 26
name = input("Name: ")
lowercase = name.lower()
print("Your 'cleaned up' name is:", lowercase)
print("Your 'cleaned up name reduces to:")
length = len(name)
name1 = 0
for x in range(name[0], name[length]):
print(name[name1])
name1 += 1
You could save yourself all those variables, and not even need a dictionary by just utilizing ord here and calculating the numerical position in the alphabet:
Example: Taking letter c, which using the following should give us 3:
>>> ord('c') - 96
3
ord will:
Return the integer ordinal of a one-character string.
The 96 is used because of the positions of the alphabet on the ascii table.
So, with that in mind. When you enter a word, using your example: "Jon"
word = input("enter name")
print(*(ord(c) - 96 for c in name.lower()))
# 10 15 14
You can store each letter and the indices in a dict so you can easily retrieve the ones in name:
>>> from string import ascii_lowercase
>>> letters = dict(zip(ascii_lowercase, range(1, len(ascii_lowercase) + 1)))
>>> for c in name:
... print(letters[c])
If you want indices lined up in the string:
>>> print(" ".join(str(letters[c]) for c in name))
"10 15 14"
You are currently passing characters to range(). range(name[0], name[length]) with a name of 'Jon' is equivalent to range('J', 'n')... or it would be if strings were 1-indexed. Unfortunately for this code snippet, a sequence does not have an element with an index equal to the sequence's length. The last element of a three-character string has an index of two, not three. Your algorithm also has zero interaction with the letter values you defined above. It has little chance of succeeding.
Rather than defining each letter's value separately, store it in a string and then look up each letter's index in that string:
name = input('Name: ')
s = 'abcdefghijklmnopqrstuvwxyz'
print(*(s.index(c)+1 for c in name.lower()))
A generator that produces the index of each of the name's characters in the master string (plus one, because you want it one-indexed) is unpacked and sent to print(), which, with the default separator of a space, produces the desired output.
Rather than define 26 different variables, how about using a dictionary? Then you can write something like:
mapping = {
'a': 1,
# etc
}
name_in_numbers = ' '.join(mapping[letter] for letter in name)
Note that this will break for any input that doesn't only contain letters.
First of all you must use a dict to store the mapping of characters to int, you may use, string module to access all the lowercase characters, it makes your code less error prone. Secondly you just need to iterate over the characters in the lowercase string and access the mapped values as int from the given mapping:
import string
mapping = dict(zip(string.ascii_lowercase, range(1, len(string.ascii_lowercase)+1)))
name = "Anmol"
lowercase = name.lower()
print("Your 'cleaned up' name is:", lowercase)
print("Your 'cleaned up name reduces to:")
for char in lowercase:
print mapping[char],
# make up a data structure to not pollute the namespace with 26 variable names; a dictionary does well for this
dict={}
for i in range(97,123):
dict[chr(i)]=i-96
print dict
name=raw_input("Name: ")
name=name.lower()
for i in name:
print dict[i],
Output:
{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4, 'g': 7, 'f': 6, 'i': 9, 'h': 8, 'k': 11, 'j': 10, 'm': 13, 'l': 12, 'o': 15, 'n': 14, 'q': 17, 'p': 16, 's': 19, 'r': 18, 'u': 21, 't': 20, 'w': 23, 'v': 22, 'y': 25, 'x': 24, 'z': 26}
Name: Jon
10 15 14
Personally, I prefer that you build a mapping of char:index, that you can always refer to later, this way:
>>> ascii_chrs = 'abcdefghijklmnopqrstuvwxyz'
>>> d = {x:i for i,x in enumerate(ascii_chrs, 1)}
>>> d
{'q': 17, 'i': 9, 'u': 21, 'x': 24, 'a': 1, 's': 19, 'm': 13, 'n': 14, 'e': 5, 'v': 22, 'b': 2, 'p': 16, 'g': 7, 'o': 15, 'j': 10, 't': 20, 'h': 8, 'f': 6, 'r': 18, 'y': 25, 'c': 3, 'k': 11, 'd': 4, 'z': 26, 'w': 23, 'l': 12}
>>>
>>> word = 'Salam'
>>> print(*(d[c] for c in word.lower()))
19 1 12 1 13

Python dictionary, adding a new value to key if present multiple times in list

Im trying to split a string of letters into a dictionary which automatically adds the value +1 for every letter present more than once.
The only problem is that my code adds the value +1 for every key...For example if i input: "aasf" the dict will be: a:2, s:2, f:2... Whats wrong??
word = raw_input("Write letters: ")
chars = {}
for c in word:
chars[c] = c.count(c)
if c in chars:
chars[c] += 1
print chars
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53)
>>> from collections import Counter
>>> Counter('count letters in this sentence')
Counter({'e': 5, 't': 5, ' ': 4, 'n': 4, 's': 3, 'c': 2, 'i': 2, 'h': 1, 'l': 1, 'o': 1, 'r': 1, 'u': 1})
>>>
you must use either
chars[c] = words.count(c)
OR
chars[c] += 1
but not both.

Categories

Resources