python - Letter Count Dict - python

Write a Python function called LetterCount() which takes a string as an argument and returns a dictionary of letter counts.
The line:
print LetterCount("Abracadabra, Monsignor")
Should produce the output:
{'a': 5, 'c': 1, 'b': 2, 'd': 1, 'g': 1, 'i': 1, 'm': 1, 'o': 2, 'n': 2, 's': 1, 'r': 3}
I tried:
import collections
c = collections.Counter('Abracadabra, Monsignor')
print c
print list(c.elements())
the answer I am getting looks like this
{'a': 4, 'r': 3, 'b': 2, 'o': 2, 'n': 2, 'A': 1, 'c: 1, 'd': 1, 'g': 1, ' ':1, 'i':1, 'M':1 ',':1's': 1, }
['A', 'a','a','a','a','c','b','b','d','g', and so on
okay now with this code
import collections
c = collections.Counter('Abracadabra, Monsignor'.lower())
print c
am getting this
{'a': 5, 'r': 3, 'b': 2, 'o': 2, 'n': 2, 'c: 1, 'd': 1, 'g': 1, ' ':1, 'i':1, ',':1's': 1, }
but answer should be this
{'a': 5, 'c': 1, 'b': 2, 'd': 1, 'g': 1, 'i': 1, 'm': 1, 'o': 2, 'n': 2, 's': 1, 'r': 3}

You are close. Note that in the task description, the case of the letters is not taken into account. They want {'a': 5}, where you have {'a': 4, 'A': 1}.
So you have to convert the string to lower case first (I'm sure you will find out how).

Use dictionary for the letter count:
s = "string is an immutable object"
d = {}
for i in s:
d[i] = d.get(i,0)+1
print d
Output:
{'a': 2, ' ': 4, 'c': 1, 'b': 2, 'e': 2, 'g': 1, 'i': 3, 'j': 1, 'm': 2, 'l': 1, 'o': 1, 'n': 2, 's': 2, 'r': 1, 'u': 1, 't': 3}

Maybe this to count just letters, exclude spaces and overlook its case and sort them alphabetically?:
t = "The cat is out of the bag."
word_count = {}
for i in t.casefold():
if i.isalnum():
word_count[i] = word_count.get(i,0)+1
for letter, count in sorted(word_count.items()):
print(letter, count)
Output:
a 2
b 1
c 1
e 2
f 1
g 1
h 2
i 1
o 2
s 1
t 4
u 1

Related

Check if a Dictionary is a Subset of another Dictionary with Key Value pairs

I have two Dictionaries resources, and available_resources:
resources = {'B': 1, 's': 2, 't': 3, 'e': 3, '!': 1, 'h': 1, 'i': 1, ' ': 3, 'o': 1, 'g': 1, 'E': 1, 'A': 1, 'x': 2, 'p': 1, 'l': 1, 'r': 1}
available_resources = {'A': 1, 'l': 1, 'g': 1, 'o': 1, 'E': 1, 'x': 1, 'p': 1, 'e': 3, 'r': 1, 't': 3, ' ': 3, 'i': 1, 's': 2, 'h': 1, 'B': 1, '!': 1}
I want to check if resources is a subset of available_resources (if each element contained in the dictionary is <= the corresponding value entry in the resources dictionary)
I've tried:
if all(available_resources.get(key, None) == val for key, val
in resources.items()):
return True
It is returning false, is there another way I can get it to work?
Could it be a simple sign error? From "==" val to "<=" val? I got true from the below.
if all(available_resources.get(key, None) <= val for key, val
in resources.items()):
return True
If all the values are integers, one approach is to use collections.Counter:
from collections import Counter
resources = {'B': 1, 's': 2, 't': 3, 'e': 3, '!': 1, 'h': 1, 'i': 1, ' ': 3, 'o': 1, 'g': 1, 'E': 1, 'A': 1, 'x': 2, 'p': 1, 'l': 1, 'r': 1}
available_resources = {'A': 1, 'l': 1, 'g': 1, 'o': 1, 'E': 1, 'x': 1, 'p': 1, 'e': 3, 'r': 1, 't': 3, ' ': 3, 'i': 1, 's': 2, 'h': 1, 'B': 1, '!': 1}
res = bool(Counter(resources) - Counter(available_resources))
print(res)
Output
True
You can use the <= operator from sets. This operator determines whether one set is a subset of the other.
As follows:
>>> resources.items() <= available_resources.items()
False
This returns False as there is a difference between the element x in the different dict. You can see this difference using the set operator ^ with will return you the symmetric difference between the dict:
>>> resources.items() ^ available_resources.items()
{('x', 1), ('x', 2)}
You need to use <= instead of ==
>>> all(available_resources.get(k, -1)<=v for k,v in resources.items())
True
Also, above method may fail if resources contains some key that doesn't exist in available_resources, and you can additionally check if the keys in resources are subset of the keys in available_resources for this condition
>>> all(available_resources.get(k, -1)<=v for k,v in resources.items()) and\
set(resources).issubset(available_resources)
True
I have tested the answers in this stackoverflow question: click here
And i think it's works for you!
all(item in available_resources.items() for item in resources.items())
# - or - #
available_resources.items() <= resources.items()

Count the frequency of letters in a string, not blank spaces, numbers, or punctuation

def count_letters(text):
result = {}
# Go through eactter in the text
for letter in text:
# Check if the letter needs to be counted or not
if letter in text:
result[letter.lower()]=result.get(letter,0)+1
# Add or increment the value in the dictionary
for k in result:
return result
print(count_letters("AaBbCc"))
# Should be {'a': 2, 'b': 2, 'c': 2}
print(count_letters("Math is fun! 2+2=4"))
# Should be {'m': 1, 'a': 1, 't': 1, 'h': 1, 'i': 1, 's': 1, 'f': 1, 'u': 1, 'n': 1}
print(count_letters("This is a sentence."))
# Should be {'t': 2, 'h': 1, 'i': 2, 's': 3, 'a': 1, 'e': 3, 'n': 2, 'c': 1}
You can use Counter with filter.
import string
from collections import Counter
Counter(filter(lambda x:x in string.ascii_letters,_str.lower()))
Counter({'m': 1,
'a': 1,
't': 1,
'h': 1,
'i': 1,
's': 1,
'f': 1,
'u': 1,
'n': 1})

How do I get the points of each word?

I'm making a program that could count the points of two or more words. How do I get the value of each point in a list in an array? I already have a dictionary of points.
points_dictionary = {
'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, '#': 0, '0':3
}
I have a list that looks like thiscurrwords = ['PEARS' 'MANGO' 'ORANGE]
I have made a code that can get the points of each letter but its output adds all the points.
for you in currwords:
for yeah in you:
trans = list(yeah)
trans = points_dictionary[yeah[0]]
total_words.append(trans)
final1 = sum(total_words)
print(final1)
Every time I use this code it only outputs the total points on what is on the list. How do I get the points specifically on each word like PEAR = 6 MANGO = 8 and ORANGE = 7
You could use sum together with map:
points_dictionary = {
'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, '#': 0, '0':3
}
currwords = ['PEARS', 'MANGO', 'ORANGE']
for word in currwords:
print(word, sum(map(lambda c: points_dictionary.get(c, 0), word)))
Output
PEARS 7
MANGO 8
ORANGE 7
As an alternative you could use a generator expression:
for word in currwords:
print(word, sum(points_dictionary.get(c, 0) for c in word))
The idea of both map and the generator expression is to map the letters of each word to the corresponding point values.
Let's do it the old school way:
points_dictionary = {
'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, '#': 0, '0':3
}
currwords = ['PEARS', 'MANGO', 'ORANGE']
sumsOfwords = []
sum = 0
i = -1
for words in currwords:
for word in words:
if words == currwords[i + 1]:
sum = sum + points_dictionary[word]
else:
sumsOfwords.append(sum)
sum = 0
sum = sum + points_dictionary[word]
i = i + 1
sumsOfwords.append(sum)
print(sumsOfwords)
OUTPUT:
[7, 8, 7]
dictionary = dict(zip(currwords, sumsOfwords))
print(dictionary)
OUTPUT:
{'PEARS': 7, 'MANGO': 8, 'ORANGE': 7}
I think the issue here is that you append trans to total_words at each iteration, but never reset its value. You could add a
total_words = []
inside of the first loop. Also, inside of your for yeah in you loop, you define trans twice, so the first one is never used. After correcting that, your code should look like this :
for you in currwords:
total_words = []
for yeah in you:
trans = points_dictionary[yeah]
total_words.append(trans)
final1 = sum(total_words)
print(final1)

python syntax error in dict comprehension

Here is what I'm trying to do:
I want to count letters frequency in a sentence.
Here is my code in python so far:
for i in line:
if i in my_count.keys():
my_count[i]+=1
else:
my_count[i]=1
Is there any way to fulfill the same goal with dict comprehension, the same we would do with list comprehension?
I have thought at something such as :
my_count = { x:(my_count[x]+=1) for x in line if x in my_count else x:1 }
But this does not pass the syntax check (SyntaxError: invalid syntax at the +=).
Thanks for your help and advices!
Counter in collections seems to accomplish this.
In [1]: line = 'the quick brown fox jumps over the lazy dog'
In [2]: from collections import Counter
In [3]: c = Counter(line)
In [4]: c
Out[4]: Counter({' ': 8, 'o': 4, 'e': 3, 'h': 2, 'r': 2, 'u': 2, 't': 2, 'a': 1, 'c': 1, 'b': 1, 'd': 1, 'g': 1, 'f': 1, 'i': 1, 'k': 1, 'j': 1, 'm': 1, 'l': 1, 'n': 1, 'q': 1, 'p': 1, 's': 1, 'w': 1, 'v': 1, 'y': 1, 'x': 1, 'z': 1})

How to sum items in a for loop in python?

I have the following code:
dict = {
'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 = 'banana'
for letter in word:
print dict[letter]
I get the following output
3
1
1
1
1
How can I add these values?
That is how can I print the output as 8
You can use sum and a generator expression:
>>> # Please don't name a variable `dict` -- it overshadows the built-in
>>> dct = { '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 = 'banana'
>>> print sum(dct[letter] for letter in word)
8
>>>
Note that the above solution assumes that all of the characters in word can be found in dct. If this isn't always the case, then you can use dict.get to avoid a KeyError:
>>> dct = { '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 = '$banana1'
>>> print sum(dct.get(letter, 0) for letter in word)
8
>>>
Here is another way that you can sum the items in the list.
dict = {
'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 = 'banana'
sum = 0
for letter in word:
sum+=dict[letter]
print sum

Categories

Resources