I am tasked with coming with a program that will decyrpt a Cesar cipher, and I was looking at other questions previously asked on this site and understand it mostly. However, I just have a very basic question on how to get a tally of every letter within a string.
here's what I have come up with so far:
Input=input("input the text you want to decipher:")
import string
print(string.ascii_uppercase)
def get_char(ch,shift):
#get a tally of the each letter
common_letter=#letter with most "tallies"
return common_letter
print(common_letter)
#finding the shift
def get_shift(s,ignore):
for x in Input:
shift=get_char-x
if shift=='e':
return x
print(x)
def output_plaintext(s,shift):
#convert back to English based off shift
pass
def main():
# main body where i call together my other functions
pass
input("is this decrypted?")
#if no is inputted re run program with second most common letter
How do I get a count of each letter in a String?
-Nathan
This may help you:-
from collections import Counter
input='Nathannn'
print Counter(input)
Output:-
Counter({'n': 3, 'a': 2, 'h': 1, 't': 1, 'N': 1})
If you want to ignore case use input.lower() and then apply Counter(input)
Here's another approach, if you can't use imported modules, e.g. collections:
>>> string = 'aardvark'
>>> {letter: string.count(letter) for letter in set(string)}
{'v': 1, 'r': 2, 'd': 1, 'a': 3, 'k': 1}
You can get the last character in a string using the following code:
string[len(string)-1]
Related
I am writing a python program to find character sequence in a word. But the program is giving the unexpected result.
I have found a similar type program that works perfectly.
To me I think the two program is quite similar but dont know why one of them does not work
The program that is not working:
# Display the character sequence in a word
dict={}
string=input("Enter the string:").strip().lower()
for letter in string:
if letter !=dict.keys():
dict[letter]=1
else:
dict[letter]=dict[letter]+1
print(dict)
The program that is working:
def char_frequency(str1):
dict = {}
for n in str1:
keys = dict.keys()
if n in keys:
dict[n] += 1
else:
dict[n] = 1
return dict
print(char_frequency('google.com'))
The output for the first program is giving:
Enter the string:google.com
{'g': 1, 'c': 1, 'm': 1, 'o': 1, 'l': 1, '.': 1, 'e': 1}
The output for the second program is:
{'c': 1, 'e': 1, 'o': 3, 'g': 2, '.': 1, 'm': 1, 'l': 1}
The above is the correct output.
Now the questions in my mind.
i. Why the first program is not working correctly?
ii. Is the ideology of these two programs are different?
Actually, there's a little mistake is in the if statement you have used. Just have a look at the below modified program.
Note: Also make sure not to use pre-defined data type names like dict as variable names. I have changed that to d here.
>>> d = {}
>>>
>>> string=input("Enter the string:").strip().lower()
Enter the string:google.com
>>>
>>> for letter in string:
... if letter not in d.keys():
... d[letter] = 1
... else:
... d[letter] = d[letter] + 1
...
>>> print(d)
{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
>>>
You can have also have a look at the below statements executed on the terminal.
Comparing a key with d.keys() will always return False as key is a string here and d.keys() will always be an object of type dict_keys (Python3) and a list (Python2).
>>> d = {"k1": "v1", "k3": "v2", "k4": "Rishi"}
>>>
>>> d.keys()
dict_keys(['k1', 'k3', 'k4'])
>>>
>>> "k1" in d
True
>>>
>>> not "k1" in d
False
>>>
>>> "k1" == d.keys()
False
>>>
>>> "k1" not in d
False
>>>
Answers of your 2 questions:
Because the statement letter != dict.keys() is always True so no increment in key counts. Just change it to letter not in dict.keys(). And it is better to use d in place of dict so that the statement will look like letter not in d.keys().
Logic of both the programs are same i.e. iterating over the dictionary, checking for an existence of key in dictionary. If it does not exist, create a new key with count 1 else increment the related count by 1.
Thank you v. much.
This line is nonsensical:
if letter !=dict.keys():
letter is a length one str, while dict.keys() returns a key view object, which is guaranteed to never be equal to a str of any kind. Your if check is always false. The correct logic would be:
if letter not in dict:
(you could add .keys() if you really want to, but it's wasteful and pointless; membership testing on a dict is checking its keys implicitly).
Side-note: You're going to confuse the crap out of yourself by naming a variable dict, because you're name-shadowing the dict constructor; if you ever need to use it, it won't be available in that scope. Don't shadow built-in names if at all possible.
I'm trying to create dictionary with the count of each letter appearing in a list of words. The method count_letters_v2(word_list), print the letters but does not return the dictionary. Why? And how do I fix this? Thank you.
def count_letters_v2(word_list):
count = {}
for word in word_list:
print word
for letter in word:
print letter
if letter not in count:
count[letter] = 1
else:
count[letter] = count[letter] + 1
return count
def main():
count_letters_v2(['a','short','list','of','words'])
if __name__ == '__main__':
main()
It does return the dictionary. That's what return count does. Do you want to print out the dictionary? Then change main() to
def main():
print count_letters_v2(['a','short','list','of','words'])
For the record, there's a Counter object (a subclass of dict so can do all the same things) in the standard library that will do this all for you.
from collections import Counter
def count_letters_v3(word_list):
return Counter(''.join(word_list))
print count_letters_v3(['a','short','list','of','words'])
Output:
Counter({'a': 1,
'd': 1,
'f': 1,
'h': 1,
'i': 1,
'l': 1,
'o': 3,
'r': 2,
's': 3,
't': 2,
'w': 1})
As said, you code works but you didn't do anytinhg with the return value of the function.
That said, I can think of some improvements though. First, the get method
of dict will make the case of a new letter cleaner, setting the default value to 0:
...
count = {}
for word in word_list:
for letter in word:
count[letter] = count.get(letter, 0) + 1
...
Otherwise, you can use a Counter object from collections:
from collections import Counter
def count_letters_v2(word_list):
count = Counter()
for word in word_list:
count.update(word)
return count
If you have a very long list of words, you shouldn't use str.join since it builds new string. The chain_from_iterable method of itertools module will chain the letters for free:
from collections import Counter
from itertools import chain
def count_letters_v2(word_list):
return Counter(chain.from_iterable(word_list))
I'm so stuck on this task. I have a task where I need to write a program in python 2.7 which prompts a user to input a string and then the program needs to return the number of times the letters in that string occur. for example the word "google.com" must return 'o': 3, 'g': 2, '.': 1, 'e': 1, 'l': 1, 'm': 1, 'c': 1
I know i need to use the list() function but all i have so far is:
string = raw_input("Enter a string: ")
newString = list(string)
and then i get stuck from there because I don't know how to make the program count the number of times the letters occur. I know there must be a for loop in the syntax but I'm not sure how I'm going to use it in this case.
NB: We haven't been introduced to dictionaries or imports yet so please keep it as simple as possible. Basically the most round about method will work best.
You can handle this problem directly with the help of count function.
You can start with an empty dictonary and add each character of the entered string and its count to the dictionary.
This can be done like this..!
string = raw_input("Enter a string: ")
count_dict = {}
for x in string:
count_dict[x] = string.count(x)
print count_dict
#input : google.com
# output : {'c': 1, 'e': 1, 'g': 2, 'm': 1, 'l': 1, 'o': 3, '.': 1}
Update:
Since you haven't been introduced to dictionary and imports, you can use the below solution.
for i in set(string):
print("'{}'".format(i), string.count(i), end=",")
Use Counter:
from collections import Counter
string = "google.com"
print(Counter(string))
Other way, create a dictionary and add chars looping through your string.
dicta = {}
for i in string:
if i not in dicta:
dicta[i] = 1
else:
dicta[i] += 1
print(dicta)
I am the biggest rookie of all rookies in python, and i want to learn how to write a code that
A) Reads and analyses a text document, and
B) Prints how many of a certain character is in the text document
For example, if the text document said 'Hello my name is Mark' it will return as
A: 2
E: 2
H: 1 etc.
To be fair, I only know how to read text files in python because I googled it no less than 3 minutes ago, so I'm working from scratch here. The only thing I have written is
txt = open("file.txt","r")
print(txt.count("A")) #an experimental line, it didnt work
file.close()
I also tried the code
txt = input("Enter text here: ")
print("A: ", txt.count("A"))
...
print("z: ", txt.count("z"))
Which would have worked if the text file didnt have speech marks in it which made the programme return only information from the things in the speech marks, hence text files.
The easiest way is using collections.Counter:
import collections
with open('file.txt') as fh:
characters = collections.Counter(fh.read())
# Most common 10 characters (probably space and newlines are the first 2)
print(characters.most_common(10))
I'm not sure what you mean by speech marks though, we can filter out all non-alphabetical characters like this:
import collections
import string
allowed_characters = set(string.ascii_letters)
with open('file.txt') as fh:
data = fh.read()
data = (c for c in data if c in allowed_characters)
characters = collections.Counter(data)
# Most common 10 characters
print(characters.most_common(10))
txt is a file handle for your first case, it is a pointer and not container of your file.
you can try this
txt = open('yourfile') # read mode is the default
content = txt.read()
print (content.count('A'))
txt.close()
Here is an example test.txt and the corresponding Python code to calculate each alphabet's frequency :
test.txt :
Red boxed memory, sonet.
This is an anonymous text.
Liverpool football club.
Two empty lines.
This is an SO (Stack Overflow) answer.
This is the Python code :
file = open('test.txt');
the_string = file.read();
alphabets = 'abcdefghijklmnopqrstuvwxyz';
Alphabets = dict();
for i in alphabets:
frequency = the_string.count(i) + the_string.count(i.capitalize());
Alphabets[i] = frequency;
print(Alphabets);
The Alphabets is therefore a dictionary of :
{'a': 6, 'b': 3, 'c': 2, 'd': 2, 'e': 10, 'f': 2, 'g': 0, 'h': 2, 'i': 6, 'j': 0, 'k': 1, 'l': 7, 'm': 4, 'n': 7, 'o': 13, 'p': 2, 'q': 0, 'r': 5, 's': 10, 't': 9, 'u': 2, 'v': 2, 'w': 3, 'x': 2, 'y': 3, 'z': 0}
You can get the frequency of an alphabet by, for example, Alphabets['a'] which will return 6. Alphabets['n'] which will return 7.
The frequency is including the capital letter, using frequency = the_string.count(i) + the_string.count(i.capitalize());.
Notice that when reading the file, each line will have an \n at the end, marking line spacing. This \n is counted as a whole, it doesn't represent a \ char and an n char. So Alphabets['n'] will not include the 'n' from \n.
Is this okay?
I have a lengthy Python list and would like to count the number of occurrences of a single character. For example, how many total times does 'o' occur? I want N=4.
lexicon = ['yuo', 'want', 'to', 'sioo', 'D6', 'bUk', 'lUk'], etc.
list.count() is the obvious solution. However, it consistently returns 0. It doesn't matter which character I look for. I have double checked my file - the characters I am searching for are definitely there. I happen to be calculating count() in a for loop:
for i in range(100):
# random sample 500 words
sample = list(set(random.sample(lexicon, 500)))
C1 = ['k']
total = sum(len(i) for i in sample) # total words
sample_count_C1 = sample.count(C1) / total
But it returns 0 outside of the for loop, over the list 'lexicon' as well. I don't want a list of overall counts so I don't think Counter will work.
Ideas?
If we take your list (the shortened version you supplied):
lexicon = ['yu', 'want', 'to', 'si', 'D6', 'bUk', 'lUk']
then we can get the count using sum() and a generator-expression:
count = sum(s.count(c) for s in lexicon)
so if c were, say, 'k' this would give 2 as there are two occurances of k.
This will work in a for-loop or not, so you should be able to incorporate this into your wider code by yourself.
With your latest edit, I can confirm that this produces a count of 4 for 'o' in your modified list.
If I understand your question correctly, you would like to count the number of occurrences of each character for each word in the list. This is known as a frequency distribution.
Here is a simple implementation using Counter
from collections import Counter
lexicon = ['yu', 'want', 'to', 'si', 'D6', 'bUk', 'lUk']
chars = [char for word in lexicon for char in word]
freq_dist = Counter(chars)
Counter({'t': 2, 'U': 2, 'k': 2, 'a': 1, 'u': 1, 'l': 1, 'i': 1, 'y': 1, 'D': 1, '6': 1, 'b': 1, 's': 1, 'w': 1, 'n': 1, 'o': 1})
Using freq_dist, you can return the number of occurrences for a character.
freq_dist.get('a')
1
# get() method returns None if character is not in dict
freq_dist.get('4')
None
It's giving zero because sample.count('K') will matches k as a string. It will not consider buk or luk.
If u want to calculate frequency of character go like this
for i in range(100):
# random sample 500 words
sample = list(set(random.sample(lexicon, 500)))
C1 = ['k']
total = sum(len(i) for i in sample) # total words
sample_count=sum([x.count(C1) for x in sample])
sample_count_C1 = sampl_count / total