I am trying to write a function in python which counts the occurrences of letters in the text shifted by x characters. The function works and saves the counted results in the dictionary, but when I want to save this dictionary to another one, the values already saved in it are written by the new dictionary that I add.
Here's my code:
def calcLetter(message):
messageWithoutSpace = message.replace(" ", "")
shift = 6
alphabet = list(string.ascii_lowercase)
position = {}
tempList = {}
for pos in range(0, 6):
for letter in alphabet:
tempList[letter] = {}
textLenght = len(messageWithoutSpace)
tempCount = 0
letterIndex = 0
while textLenght > 0:
if len(messageWithoutSpace) - pos > letterIndex:
if messageWithoutSpace[pos + letterIndex] == letter:
tempCount += 1
letterIndex = letterIndex + shift
textLenght = textLenght - shift
tempList[letter] = tempCount
print("Start position:", pos)
print(tempList)
position[pos] = tempList
print(position)
When I check it for each index, the letters are well counted, but only the last calculations are saved in the final dictionary.
Result:
Start position: 0
{'a': 16, 'b': 1, 'c': 3, 'd': 1, 'e': 10, 'f': 2, 'g': 5, 'h': 5, 'i': 0, 'j': 11, 'k': 7, 'l': 4, 'm': 0, 'n': 9, 'o': 8, 'p': 7, 'q': 3, 'r': 0, 's': 4, 't': 0, 'u': 6, 'v': 7, 'w': 5, 'x': 2, 'y': 5, 'z': 3}
Start position: 1
{'a': 1, 'b': 6, 'c': 1, 'd': 0, 'e': 7, 'f': 20, 'g': 3, 'h': 0, 'i': 7, 'j': 4, 'k': 8, 'l': 2, 'm': 0, 'n': 4, 'o': 0, 'p': 6, 'q': 7, 'r': 10, 's': 3, 't': 5, 'u': 4, 'v': 17, 'w': 1, 'x': 0, 'y': 3, 'z': 4}
Start position: 2
{'a': 4, 'b': 6, 'c': 12, 'd': 6, 'e': 0, 'f': 9, 'g': 4, 'h': 3, 'i': 2, 'j': 0, 'k': 2, 'l': 0, 'm': 4, 'n': 9, 'o': 12, 'p': 2, 'q': 2, 'r': 8, 's': 6, 't': 0, 'u': 5, 'v': 1, 'w': 11, 'x': 2, 'y': 7, 'z': 6}
Start position: 3
{'a': 13, 'b': 6, 'c': 2, 'd': 5, 'e': 2, 'f': 11, 'g': 0, 'h': 2, 'i': 0, 'j': 11, 'k': 1, 'l': 1, 'm': 3, 'n': 2, 'o': 7, 'p': 12, 'q': 2, 'r': 0, 's': 8, 't': 11, 'u': 5, 'v': 3, 'w': 0, 'x': 8, 'y': 0, 'z': 8}
Start position: 4
{'a': 7, 'b': 0, 'c': 6, 'd': 8, 'e': 14, 'f': 0, 'g': 4, 'h': 3, 'i': 9, 'j': 2, 'k': 3, 'l': 1, 'm': 10, 'n': 0, 'o': 5, 'p': 4, 'q': 10, 'r': 7, 's': 9, 't': 0, 'u': 0, 'v': 6, 'w': 6, 'x': 9, 'y': 0, 'z': 0}
Start position: 5
{'a': 2, 'b': 0, 'c': 5, 'd': 4, 'e': 5, 'f': 5, 'g': 0, 'h': 4, 'i': 0, 'j': 7, 'k': 5, 'l': 9, 'm': 0, 'n': 7, 'o': 6, 'p': 16, 'q': 0, 'r': 2, 's': 3, 't': 14, 'u': 2, 'v': 3, 'w': 4, 'x': 1, 'y': 10, 'z': 9}
{0: {'a': 2, 'b': 0, 'c': 5, 'd': 4, 'e': 5, 'f': 5, 'g': 0, 'h': 4, 'i': 0, 'j': 7, 'k': 5, 'l': 9, 'm': 0, 'n': 7, 'o': 6, 'p': 16, 'q': 0, 'r': 2, 's': 3, 't': 14, 'u': 2, 'v': 3, 'w': 4, 'x': 1, 'y': 10, 'z': 9}, 1: {'a': 2, 'b': 0, 'c': 5, 'd': 4, 'e': 5, 'f': 5, 'g': 0, 'h': 4, 'i': 0, 'j': 7, 'k': 5, 'l': 9, 'm': 0, 'n': 7, 'o': 6, 'p': 16, 'q': 0, 'r': 2, 's': 3, 't': 14, 'u': 2, 'v': 3, 'w': 4, 'x': 1, 'y': 10, 'z': 9}, 2: {'a': 2, 'b': 0, 'c': 5, 'd': 4, 'e': 5, 'f': 5, 'g': 0, 'h': 4, 'i': 0, 'j': 7, 'k': 5, 'l': 9, 'm': 0, 'n': 7, 'o': 6, 'p': 16, 'q': 0, 'r': 2, 's': 3, 't': 14, 'u': 2, 'v': 3, 'w': 4, 'x': 1, 'y': 10, 'z': 9}, 3: {'a': 2, 'b': 0, 'c': 5, 'd': 4, 'e': 5, 'f': 5, 'g': 0, 'h': 4, 'i': 0, 'j': 7, 'k': 5, 'l': 9, 'm': 0, 'n': 7, 'o': 6, 'p': 16, 'q': 0, 'r': 2, 's': 3, 't': 14, 'u': 2, 'v': 3, 'w': 4, 'x': 1, 'y': 10, 'z': 9}, 4: {'a': 2, 'b': 0, 'c': 5, 'd': 4, 'e': 5, 'f': 5, 'g': 0, 'h': 4, 'i': 0, 'j': 7, 'k': 5, 'l': 9, 'm': 0, 'n': 7, 'o': 6, 'p': 16, 'q': 0, 'r': 2, 's': 3, 't': 14, 'u': 2, 'v': 3, 'w': 4, 'x': 1, 'y': 10, 'z': 9}, 5: {'a': 2, 'b': 0, 'c': 5, 'd': 4, 'e': 5, 'f': 5, 'g': 0, 'h': 4, 'i': 0, 'j': 7, 'k': 5, 'l': 9, 'm': 0, 'n': 7, 'o': 6, 'p': 16, 'q': 0, 'r': 2, 's': 3, 't': 14, 'u': 2, 'v': 3, 'w': 4, 'x': 1, 'y': 10, 'z': 9}}
This happens because python dictionaries are mutable. You are not making a copy of the dictionary by adding it to your list, you are just referencing the object (and later making changes). Because your list contains repeated references to the same object, the content of all list entries changes.
Try importing copy and changing position[pos] = tempList to position[pos] = copy.copy(tempList).
you need to copy tempList
>>> d = {1: '1', 2: '2'}
>>> l = [d, d.copy()]
>>> d[1] = '3'
>>> l
[{1: '3', 2: '2'}, {1: '1', 2: '2'}]
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)
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