Adding value depending on dictionary value - python

Say I have a dictionary like this,
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
}
Now, say I have a variable like this.
letter = 'i'
I want to add the corresponding value to the variable score. So since letter is equal to i, then score should equal 1

SCRABBLE_LETTER_VALUES = {'a': 1, 'c': 3, 'b': 3, 'e': 1, 'd': 2, 'g': 2, 'f': 4, 'i': 1, 'h': 4, 'k': 5, 'j': 8, 'm': 3, 'l': 1, 'o': 1, 'n': 1, 'q': 10, 'p': 3, 's': 1, 'r': 1, 'u': 1, 't': 1, 'w': 4, 'v': 4, 'y': 4, 'x': 8, 'z': 10}
>>> SCRABBLE_LETTER_VALUES['i']
1
>>> SCRABBLE_LETTER_VALUES['z']
10

Dictionary values are accessed using dictionary_name[key]. So in this case:
score+=SCRABBLE_LETTER_VALUES[letter]
You also need to assign score to something before you do this:
score=0
You might find it useful to read the documentation on dictionaries: https://docs.python.org/3/tutorial/datastructures.html#dictionaries

Firstly you need to know how python plays with dictionary- the question you asked in very simple thus people are downvoting!
Answering you question:
You have the following code in mind and struggling for the rest. I will be answering them accordingly under your statements.
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
}
letter = 'i'
In python to get value for a corresponding key it work like dictionary[key], which gives the output.
So for your problem if we do SCRABBLE_LETTER_VALUES[letter], its the same as doing SCRABBLE_LETTER_VALUES['i'] where 'i' is the key and we will get 1 as output.
Hence for SCRABBLE_LETTER_VALUES[letter] we get 1 as output!
I want to add the corresponding value to the variable score. So since letter is equal to i
Assigning the corresponding value to the variable score is score = SCRABBLE_LETTER_VALUES[letter]
then score should equal 1
and we know now for sure that the value of score is 1. Don't we?
For SCRABBLE_LETTER_VALUES[letter] value is 1 when letter= 'i' thus score is 1

Related

The dictionary values created in the loop are overwritten

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'}]

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)

How to convert list of tuples to dict without losing order [duplicate]

This question already has answers here:
Ordered Dict, preserve initial Order
(3 answers)
Closed 5 years ago.
I want to count the letter frequency in a string using Dict, sorted in alphabetical order. The output should be a dictionary only.
Here's my attempt.
s = raw_input()
arr = {}
for i in s :
if i in arr :
arr[i] += 1
else :
arr[i] = 1
x = sorted(arr.items())
print dict(x)
Input :
amphisofttechnologies
Expected Output :
{'a': 1, 'c': 1, 'e': 2, 'f': 1, 'g': 1, 'h': 2, 'i': 2, 'l': 1, 'm': 1, 'n': 1, 'o': 3, 'p': 1, 's': 2, 't': 2}
My Output :
{'a': 1, 'c': 1, 'e': 2, 'g': 1, 'f': 1, 'i': 2, 'h': 2, 'm': 1, 'l': 1, 'o': 3, 'n': 1, 'p': 1, 's': 2, 't': 2}
Kindly point out where I am making the mistake.
Python dict explicitly doesn't guarantee/keep order between keys. So what you are asking can't be done.
You're looking for OrderedDict.

"histogram" function: input strings and output dictionary

I'm trying to create a histogram function that takes strings and counts the number of times the strings was used and put into a dictionary. I'm still learning Python so any tips would be helpful.
>>> histogram('The Goose that Laid the Golden Egg')
{'l': 2, 'n': 1, 'o': 3, 'h': 3, 'i': 1,'d': 2, 'e': 5, 'g': 4, ' ': 6, 'a': 2, 't': 4, 's': 1}
Its what that collections.Counter is for :
>>> from collections import Counter
>>> Counter('The Goose that Laid the Golden Egg')
Counter({' ': 6, 'e': 4, 'h': 3, 'o': 3, 't': 3, 'a': 2, 'd': 2, 'G': 2, 'g': 2, 'i': 1, 'L': 1, 'l': 1, 's': 1, 'T': 1, 'E': 1, 'n': 1})
I won't solve this for you, but will give you a hint: use collections.Counter. Combine this with the fact that strings are iterable, and this gets you very close to a solution.

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