Get the number of occurrences of each character - python

Given the string:
a='dqdwqfwqfggqwq'
How do I get the number of occurrences of each character?

In 2.7 and 3.1 there's a tool called Counter:
>>> import collections
>>> results = collections.Counter("dqdwqfwqfggqwq")
>>> results
Counter({'q': 5, 'w': 3, 'g': 2, 'd': 2, 'f': 2})
Docs. As pointed out in the comments it is not compatible with 2.6 or lower, but it's backported.

Not highly efficient, but it is one-line...
In [24]: a='dqdwqfwqfggqwq'
In [25]: dict((letter,a.count(letter)) for letter in set(a))
Out[25]: {'d': 2, 'f': 2, 'g': 2, 'q': 5, 'w': 3}

You can do this:
listOfCharac={}
for s in a:
if s in listOfCharac.keys():
listOfCharac[s]+=1
else:
listOfCharac[s]=1
print (listOfCharac)
Output {'d': 2, 'f': 2, 'g': 2, 'q': 5, 'w': 3}
This method is efficient as well and is tested for python3.

For each letter count the difference between string with that letter and without it, that way you can get it's number of occurences
a="fjfdsjmvcxklfmds3232dsfdsm"
dict(map(lambda letter:(letter,len(a)-len(a.replace(letter,''))),a))

lettercounts = {}
for letter in a:
lettercounts[letter] = lettercounts.get(letter,0)+1

python code to check the occurrences of each character in the string:
word = input("enter any string = ")
for x in set(word):
word.count(x)
print(x,'= is', word.count(x))
Please try this code if any issue or improvements please comments.

one line code for finding occurrence of each character in string.
for i in set(a):print('%s count is %d'%(i,a.count(i)))

Related

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}

How does this for loop work on the following string?

I have been working through Automate the Boring Stuff by Al Sweighart. I'm struggling with understanding the code below:
INPUT
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)
OUTPUT
{'I': 1, 't': 6, ' ': 13, 'w': 2, 'a': 4, 's': 3, 'b': 1, 'r': 5, 'i': 6, 'g': 2, 'h': 3, 'c': 3, 'o': 2, 'l': 3, 'd': 3, 'y': 1, 'n': 4, 'A': 1, 'p': 1, ',': 1, 'e': 5, 'k': 2, '.': 1}
QUESTION
Since it does not matter what the variable in a for loop is called (ie character can be changed to x, pie etc) how does the code know to run the loop through each character in the string?
It's not about the variable's name, it's about the object this variable points to. The implementation of the loop in the Python virtual machine knows how to iterate over objects based on their types.
Iterating over something is implemented as iterating over iter(something), which in turn is the same as iterating over something.__iter__(). Different classes implement their own versions of __iter__, so that loops work correctly.
str.__iter__ iterates over the individual characters of a string, list.__iter__ - over the list's elements and so on.
You could create your own object and iterate over it:
class MyClass:
def __iter__(self):
return iter([1,2,3,4])
my_object = MyClass()
for x in my_object:
print(x)
This will print the numbers from 1 to 4.
A string is an array in python. So, it means that when you loop on a string, you loop on each character; in your case, you set what has been read to character.
Then, setdefault maps character to 0 if character is not yet in the dict. The rest looks quite straightforward.
Strings in python are sequences of chars : https://docs.python.org/3/library/stdtypes.html#textseq. Therefore, the for c in m: line iterate on every elements of the m sequence, i.e. on every character of the string

Assign integers to alphabets and add those integers

I am trying to assign numbers 1-26 to alphabets a-z and add up those numbers according to any given string without any success. For example: a = 1, b=2, c=3. So, if any given string is "abc", the output should be 1+2+3=6.
Programming background - Novice, self-learning.
I have only learned upto strings, lists and their corresponding methods in python programming. I haven't studied functions and classes yet, so please make your answers as simple as possible.
So far I've tried
Name = "abc"
a,b,c = [1,2,3]
Sum_of_name = ""
For alphabet in abc:
Sum_of_name = sum_of_name + alphabet
Print(sum_of_name)
Prints out the same abc.
I realise that when I iterate the string "abc", the string is different than the variables a,b and c. Thus, the integers aren't assigned to the strings and can't be added up.
Any suggestions on how I can work through this with my current level of knowledge.
This is one approach.
Demo:
from string import ascii_lowercase
d = {v: i for i,v in enumerate(ascii_lowercase, 1)}
Name = "abc"
print( sum(d[i] for i in Name) )
Output:
6
First make a list of the letters
>>> from string import ascii_lowercase as alphabet
>>> alphabet
'abcdefghijklmnopqrstuvwxyz'
Then make a lookup of letter to value (there are other ways to do this)
>>> values = {letter: value for value, letter in enumerate(alphabet, 1)}
>>> values
{'d': 4, 'f': 6, 'o': 15, 'b': 2, 's': 19, 'c': 3, 'w': 23, 'q': 17, 'v': 22, 'p': 16, 'i': 9, 'e': 5, 'l': 12, 't': 20, 'y': 25, 'n': 14, 'a': 1, 'r': 18, 'j': 10, 'x': 24, 'g': 7, 'm': 13, 'k': 11, 'h': 8, 'z': 26, 'u': 21}
Then use that to sum values
def sum_letters(word):
return sum(values[letter] for letter in word)
>>> sum_letters('abc')
6
If you have a fixed order then you can use ord()
a="Name"
s=0
for i in a.lower():
s+=ord(i)-96
print(s)
To get the characters in the alphabet, you can use the string lib:
>>> import string
>>> letters = string.lowercase
>>> letters
'abcdefghijklmnopqrstuvwxyz'
We can then turn that into a dictionary to make getting the numeric (positional) value of a letter easy:
letter_map = dict(zip(list(letters), range(1, len(letters) + 1)))
So your function will perform a simple dict lookup for each letter input:
def string_sum(string_input):
return sum(letter_map[char] for char in string_input)
Several test cases:
>>> assert string_sum('abc') == 6
>>> assert string_sum('') == 0 # because it's empty

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