Character counting function giving an output for each successive character - python

So i have this code that is supposed to count the characters in a user inputted sentece
import pprint
message = str(input())
count = {}
for character in message:
count.setdefault(character, 0)
count[character] = count[character] + 1
pprint.pprint(count)
however the problem is, it gives an output for every successive character i.e if you give a sentence with 3 characters in it e.g "the" it will give 3 outputs
the
{'t': 1}
{'h': 1, 't': 1}
{'e': 1, 'h': 1, 't': 1}
Process finished with exit code 0
how do i get it only to give the final output with all characters counted? thanks

You're getting multiple outputs because the print (pprint.pprint) is in a for loop.
Just remove the indentation from the pprint.pprint(count) line, so that it isn't in the for loop:
import pprint
message = str(input())
count = {}
for character in message:
count.setdefault(character, 0)
count[character] = count[character] + 1
pprint.pprint(count)

You print the output every line so this is expected. You can achieve the same with much simpler code using Counter -
from collections import Counter
import pprint
message = str(input())
count = Counter(message)
pprint.pprint(dict(count))

import pprint
message = str(input())
count = {}
for character in message:
count.setdefault(character, 0)
count[character] = count[character] + 1
pprint.pprint(count)

you just have to take off pprint.pprint(count) from for cycle
import pprint
message = str(input())
count = {}
for character in message:
count.setdefault(character, 0)
count[character] = count[character] + 1
pprint.pprint(count)
output:
messaggio
{'a': 1, 'e': 1, 'g': 2, 'i': 1, 'm': 1, 'o': 1, 's': 2}

If I understood your objective correctly; this might work. if character.isalpha() filters non alphabetic characters in the string like !"#$%&'()*+,-./:;<=>?#[\]^_{|}~` and white spaces only take into account the alphabetic letters. 
from pprint import pprint
message = 'This is a test string to check whether character to occurences mapping works correctly' #str(input())
occurrences_dict = {}
for character in message:
if character.isalpha() and character not in occurrences_dict:
occurrences_dict[character] = message.count(character)
pprint(occurrences_dict)
{'T': 1,
'a': 4,
'c': 9,
'e': 8,
'g': 2,
'h': 5,
'i': 4,
'k': 2,
'l': 1,
'm': 1,
'n': 3,
'o': 5,
'p': 2,
'r': 8,
's': 6,
't': 8,
'u': 1,
'w': 2,
'y': 1}

Related

what is "count[character] = count[character] + 1" doing in this program to count letters in a sentence

The line of code below is meant to count the number of individual letters in the sentence assigned to the variable "message".
message = 'It was a bright cold day in April, and the clocks were striking thirteen'
count = {}
for character in message:
count.setdefault(character, 0)
count[character] = count[character] + 1
print(count)
The code runs successfully with the output below
{' ': 13, ',': 1, '.': 1, 'A': 1, 'I': 1, 'a': 4, 'c': 3, 'b': 1, 'e': 5, 'd': 3, 'g': 2,
'i': 6, 'h': 3, 'k': 2, 'l': 3, 'o': 2, 'n': 4, 'p': 1, 's': 3, 'r': 5, 't': 6, 'w': 2, 'y': 1}
Please what is this part of the code doing in the program
count[character] = count[character] + 1
The line of code you mention in your question:
Extracts the existing dictionary value for the given key (character) and increments it by 1 count[character] + 1.
Updates the value for the key count[character] = .
You may actually make your code a bit shorter as setdefault method returns the current value for a given key:
message = 'It was a bright cold day in April, and the clocks were striking thirteen'
count = {}
for character in message:
count[character] = count.setdefault(character, 0) + 1
print(count)
count[character] = count[character] + 1 is essentially increasing the count of the current character by one. So, first of all, setdefault() is a function that checks if the current count exists in the dictionary. If it doesn't, it will be added with the default value of 0. Then in the dictionary, that character will be incremented by one. Let me give an example. If we are on the first (zeroth) character in message, 'I', setdefault() checks if 'I' is already in the dictionary, then adds it with the value of 0. Then it is incremented by one. That means the value of 'I' in the dictionary is now 1.

How do I count the most repeated character in a sentence in Python? [duplicate]

This question already has answers here:
How do I count the occurrences of a list item?
(29 answers)
Closed 2 years ago.
I am trying to count the occurrences of each letter of a word
word = input("Enter a word")
Alphabet=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
for i in range(0,26):
print(word.count(Alphabet[i]))
This currently outputs the number of times each letter occurs including the ones that don't.
How do I list the letters vertically with the frequency alongside it, e.g., like the following?
word="Hello"
H 1
E 1
L 2
O 1
from collections import Counter
counts=Counter(word) # Counter({'l': 2, 'H': 1, 'e': 1, 'o': 1})
for i in word:
print(i,counts[i])
Try using Counter, which will create a dictionary that contains the frequencies of all items in a collection.
Otherwise, you could do a condition on your current code to print only if word.count(Alphabet[i]) is greater than 0, though that would be slower.
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'))
As Pythonista said, this is a job for collections.Counter:
from collections import Counter
print(Counter('cats on wheels'))
This prints:
{'s': 2, ' ': 2, 'e': 2, 't': 1, 'n': 1, 'l': 1, 'a': 1, 'c': 1, 'w': 1, 'h': 1, 'o': 1}
s = input()
t = s.lower()
for i in range(len(s)):
b = t.count(t[i])
print("{} -- {}".format(s[i], b))
An easy and simple solution without a library:
string = input()
f = {}
for i in string:
f[i] = f.get(i,0) + 1
print(f)
Here is the link for get(): https://docs.quantifiedcode.com/python-anti-patterns/correctness/not_using_get_to_return_a_default_value_from_a_dictionary.html
Following up what LMc said, your code was already pretty close to functional. You just needed to post-process the result set to remove 'uninteresting' output. Here's one way to make your code work:
#!/usr/bin/env python
word = raw_input("Enter a word: ")
Alphabet = [
'a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y','z'
]
hits = [
(Alphabet[i], word.count(Alphabet[i]))
for i in range(len(Alphabet))
if word.count(Alphabet[i])
]
for letter, frequency in hits:
print letter.upper(), frequency
But the solution using collections.Counter is much more elegant/Pythonic.
For future references: When you have a list with all the words you want, lets say wordlistit's pretty simple
for numbers in range(len(wordlist)):
if wordlist[numbers][0] == 'a':
print(wordlist[numbers])
Another way could be to remove repeated characters and iterate only on the unique characters (by using set()) and then counting the occurrence of each unique character (by using str.count())
def char_count(string):
freq = {}
for char in set(string):
freq[char] = string.count(char)
return freq
if __name__ == "__main__":
s = "HelloWorldHello"
print(char_count(s))
# Output: {'e': 2, 'o': 3, 'W': 1, 'r': 1, 'd': 1, 'l': 5, 'H': 2}
It might make sense to include all letters of the alphabet. For example, if you're interested in calculating the cosine difference between word distributions you typically require all letters.
You can use this method:
from collections import Counter
def character_distribution_of_string(pass_string):
letters = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
chars_in_string = Counter(pass_string)
res = {}
for letter in letters:
if(letter in chars_in_string):
res[letter] = chars_in_string[letter]
else:
res[letter] = 0
return(res)
Usage:
character_distribution_of_string("This is a string that I want to know about")
Full Character Distribution
{'a': 4,
'b': 1,
'c': 0,
'd': 0,
'e': 0,
'f': 0,
'g': 1,
'h': 2,
'i': 3,
'j': 0,
'k': 1,
'l': 0,
'm': 0,
'n': 3,
'o': 3,
'p': 0,
'q': 0,
'r': 1,
's': 3,
't': 6,
'u': 1,
'v': 0,
'w': 2,
'x': 0,
'y': 0,
'z': 0}
You can extract the character vector easily:
list(character_distribution_of_string("This is a string that I want to know about").values())
giving...
[4, 1, 0, 0, 0, 0, 1, 2, 3, 0, 1, 0, 0, 3, 3, 0, 0, 1, 3, 6, 1, 0, 2, 0, 0, 0]
Initialize an empty dictionary and iterate over every character of the word. If the current character in present in the dictionary, increment its value by 1, and if not, set its value to 1.
word="Hello"
characters={}
for character in word:
if character in characters:
characters[character] += 1
else:
characters[character] = 1
print(characters)
import string
word = input("Enter a word: ")
word = word.lower()
Alphabet=list(string.ascii_lowercase)
res = []
for i in range(0,26):
res.append(word.count(Alphabet[i]))
for i in range (0,26):
if res[i] != 0:
print(str(Alphabet[i].upper()) + " " + str(res[i]))
If using libraries or built-in functions is to be avoided then the following code may help:
s = "aaabbc" # Sample string
dict_counter = {} # Empty dict for holding characters
# as keys and count as values
for char in s: # Traversing the whole string
# character by character
if not dict_counter or char not in dict_counter.keys(): # Checking whether the dict is
# empty or contains the character
dict_counter.update({char: 1}) # If not then adding the
# character to dict with count = 1
elif char in dict_counter.keys(): # If the character is already
# in the dict then update count
dict_counter[char] += 1
for key, val in dict_counter.items(): # Looping over each key and
# value pair for printing
print(key, val)
Output:
a 3
b 2
c 1
def string(n):
a=list()
n=n.replace(" ","")
for i in (n):
c=n.count(i)
a.append(i)
a.append(c)
y=dict(zip(*[iter(a)]*2))
print(y)
string("Lets hope for better life")
#Output:{'L': 1, 'e': 5, 't': 3, 's': 1, 'h': 1, 'o': 2, 'p': 1, 'f': 2, 'r': 2, 'b': 1, 'l': 1, 'i': 1}
(if u notice in output 2 L-letter one uppercase and other lowercase..if u want them together look for the code below)
In the output, it removes repeated characters, drops empty spaces and iterates only on the unique characters.
IF you want to count both uppercase and lowercase together the:
def string(n):
n=n.lower() #either use (n.uperr())
a=list()
n=n.replace(" ","")
for i in (n):
c=n.count(i)
a.append(i)
a.append(c)
y=dict(zip(*[iter(a)]*2))
print(y)
string("Lets hope for better life")
#output:{'l': 2, 'e': 5, 't': 3, 's': 1, 'h': 1, 'o': 2, 'p': 1, 'f': 2, 'r': 2, 'b': 1, 'i': 1}

Initializing first instance of variable in dictionary

In JavaScript, if I need to keep a counter of each individual character of a string and store it in a dictionary, I can initialize the first instance of that character to 1 before incrementing it. For example:
const frequency = {};
const place = "Disney World";
for (const c of place) {
frequency[c] = (frequency[c] + 1 || 1);
}
Is it possible to do something like this in Python?
Yes, you can use a defaultdict for this:
from collections import defaultdict
frequency = defaultdict(lambda:1)
Now keys will have automatically have a value of 1.
For references, there is a class for that in the standard library:
from collections import Counter
place = "Disney World"
frequency = Counter(place)
You directly get a dictionary like object where the letters are the keys and the value the counts.
You can check if that letter is an existing key in the dictionary, and if not, create it and set its value to 1, else increment the value like this:
frequency = {};
place = "Disney World";
for c in place:
if c in frequency:
frequency[c] += 1
else:
frequency[c] = 1
print(frequency)
# {'D': 1, 'i': 1, 's': 1, 'n': 1, 'e': 1, 'y': 1, ' ': 1, 'W': 1, 'o': 1, 'r': 1, 'l': 1, 'd': 1}
EDIT:
Following the mention of defaultdict, here is a full example of using it:
from collections import defaultdict
# using 0 so, when incremented, the first value becomes 1
frequency = defaultdict(int)
for c in place:
frequency[c] += 1
print(dict(frequency))
# {'D': 1, 'i': 1, 's': 1, 'n': 1, 'e': 1, 'y': 1, ' ': 1, 'W': 1, 'o': 1, 'r': 1, 'l': 1, 'd': 1}

Python - Ready text file and report count of individual characters

I have a Python script that prints out a randomly generated password, then prints prints it to a text file, adding a new row every time it is called. For example:
PSWD = (str(pswd01)) + (str(pswd02)) + (str(pswd03)) + (str(pswd04))
# Note, variables pswd01, pswd02 etc are randomly created earier in the script.
print(PSWD)
with open('PSWD_output.txt','a') as f:
f.write(PSWD + '\n')
f.close()
Note, the variable PSWD contains lower case, upper case, and numbers.
I then want to read the file and count the number of individual characters, and print a report to a different text file, but not sure how I can do this. I have asked a similar question here, which answers how to print a character report to the console.
Any idea how I can read PSWD_output.txt, and count each different character, writing the result to a separate text file?
Any help appreciated!
Use dictionaries to count characters repeat as below:
my_dictionary = {}
f = open('PSWD_output.txt','r')
line = f.readline()
while(line):
for letter in line:
if letter in my_dictionary:
my_dictionary[letter] +=1
else:
my_dictionary[letter] = 1
line = f.readline()
print my_dictionary
For a text file containing:
salam
asjf
asjg;
asdkj
14kj'asdf
5
returns:
>>> ================================ RESTART ================================
>>>
{'a': 6, 'j': 4, 'd': 2, 'g': 1, 'f': 2, 'k': 2, '\n': 6, 'm': 1, 'l': 1, "'": 1, '1': 1, 's': 5, '5': 1, '4': 1, ';': 1}
>>>
You can use a Counter. It takes an iterable (and strings are iterable: iterate over the characters) and stores the result as a dictionary
>>> from collections import Counter
>>> c = Counter("foobarbaz")
>>> c
Counter({'o': 2, 'a': 2, 'b': 2, 'z': 1, 'f': 1, 'r': 1})
>>> c.most_common(2)
[('o', 2), ('a', 2)]

dictionaries in python: summing the values returned from searching the keys

I am just learning Python so this is probably very simple. I am trying to find the values that match the keys in a dictionary and add them up. I have written the code which finds the values and I can print it ( tested this out in Online Python Tutor to see what happens) But I can't figure out how to just get this as a total score which returns the correct score (6). I know this is not a function at the moment.
SCRABBLE_LETTER_VALUES = {
'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1, 'f': 4, 'g': 2, 'h': 4, 'i': 1, 'j': 8, 'k': 5, 'l': 1, 'm': 3, 'n': 1, 'o': 1, 'p': 3, 'q': 10, 'r': 1, 's': 1, 't': 1, 'u': 1, 'v': 4, 'w': 4, 'x': 8, 'y': 4, 'z': 10
}
word ='tact'
score =0
for i in range(len(word)):
for letter,score in SCRABBLE_LETTER_VALUES.items():
if letter == word[i]:
print score
>>> sum(SCRABBLE_LETTER_VALUES[l] for l in word)
6
Here:
for l in word iterates over the letters of word;
SCRABBLE_LETTER_VALUES[l] gets the corresponding entries of SCRABBLE_LETTER_VALUES;
sum(...) adds them up.
The construct inside sum() is called a generator expression.
If I were you, I would use this piece of code:
score = 0
word = 'tact'
for letter in word:
score += SCRABBLE_LETTER_VALUES[letter]
print score
There are more effective ways too, for example the one mentioned by NPE, but if you're just a beginner, I would prefer using (and understanding) this method.
What the above code does, line by line:
1) First we use a for-loop to iterate over each letter in your word:
for letter in word:
2) For each letter we increment score variable by the corresponding amount you've defined in SCRABBLE_LETTER_VALUES like so:
score = score + SCRABBLE_LETTER_VALUES[letter]
This can be written easier with +=:
score += SCRABBLE_LETTER_VALUES[letter]
3) Finally we print the score:
print score

Categories

Resources