The dictionary values created in the loop are overwritten - python

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

Related

Vowel count for each line of txt file

Trying to count the number of vowels in each line of a text file. So far I have the following;
lc=0
lst=[]
vowel_count={}
with open('frankenstein.txt', 'r') as f:
for line in f:
no_of_characters=len(line)
l1=line.lower()
lc+=1
if no_of_characters !=1:
for vowel in "aeiou":
count=l1.count(vowel)
vowel_count[vowel]=count
print(lc, vowel_count)
print(lc, vowel_count)
I want the output to display the number of vowels and the corresponding line number, which is the purpose of the "lc" variable.
However when the code runs it gives the following output;
2128 {'a': 2, 'e': 6, 'i': 0, 'o': 0, 'u': 1}
2128 {'a': 2, 'e': 6, 'i': 5, 'o': 0, 'u': 1}
2128 {'a': 2, 'e': 6, 'i': 5, 'o': 4, 'u': 1}
2128 {'a': 2, 'e': 6, 'i': 5, 'o': 4, 'u': 3}
2129 {'a': 3, 'e': 6, 'i': 5, 'o': 4, 'u': 3}
2129 {'a': 3, 'e': 6, 'i': 5, 'o': 4, 'u': 3}
2129 {'a': 3, 'e': 6, 'i': 4, 'o': 4, 'u': 3}
2129 {'a': 3, 'e': 6, 'i': 4, 'o': 6, 'u': 3}
2129 {'a': 3, 'e': 6, 'i': 4, 'o': 6, 'u': 2}
2130 {'a': 3, 'e': 6, 'i': 4, 'o': 6, 'u': 2}
2130 {'a': 3, 'e': 5, 'i': 4, 'o': 6, 'u': 2}
2130 {'a': 3, 'e': 5, 'i': 2, 'o': 6, 'u': 2}
2130 {'a': 3, 'e': 5, 'i': 2, 'o': 10, 'u': 2}
2130 {'a': 3, 'e': 5, 'i': 2, 'o': 10, 'u': 3}
2131 {'a': 3, 'e': 5, 'i': 2, 'o': 10, 'u': 3}
2131 {'a': 3, 'e': 8, 'i': 2, 'o': 10, 'u': 3}
2131 {'a': 3, 'e': 8, 'i': 2, 'o': 10, 'u': 3}
2131 {'a': 3, 'e': 8, 'i': 2, 'o': 6, 'u': 3}
2131 {'a': 3, 'e': 8, 'i': 2, 'o': 6, 'u': 3}
2132 {'a': 3, 'e': 8, 'i': 2, 'o': 6, 'u': 3}
I get get several outputs for each line, how do i stop this?
You're printing status after each new found vowel. Move your print out of the inner for loop.

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 can I create an histogram in python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a distribution here for each codon: 'AAA', 'AAC' etc, in terms of letters representing amino acids
The distribution is arranged in a dictionary. How can I create an histogram that includes the distribution for each of the 64 codons?
[['AAA', {'R': 3, 'I': 3, 'S': 5, 'Y': 4, 'P': 4, 'L': 6, 'K': 5, 'D': 9, 'A': 9, 'T': 3, 'G': 3, 'N': 5, 'M': 3, 'W': 7, 'H': 5, 'V': 6, 'Q': 2, 'E': 4, 'F': 6, 'C': 5}], ['AAC', {'R': 7, 'I': 4, 'S': 5, 'Y': 4, 'P': 6, 'L': 4, 'K': 6, 'D': 5, 'A': 6, 'T': 3, 'G': 5, 'N': 5, 'M': 4, 'W': 2, 'H': 3, 'V': 6, 'Q': 7, 'E': 5, 'F': 5, 'C': 5}], ['AAG', {'R': 5, 'I': 6, 'S': 6, 'Y': 3, 'P': 8, 'L': 4, 'K': 6, 'D': 5, 'A': 6, 'T': 4, 'G': 2, 'N': 4, 'M': 2, 'W': 2, 'H': 5, 'V': 7, 'Q': 8, 'E': 5, 'F': 2, 'C': 7}], ['AAU', {'R': 4, 'I': 7, 'S': 3, 'Y': 6, 'P': 7, 'L': 1, 'K': 6, 'D': 3, 'A': 4, 'T': 6, 'G': 6, 'N': 5, 'M': 4, 'W': 5, 'H': 5, 'V': 2, 'Q': 3, 'E': 7, 'F': 6, 'C': 7}], ['ACA', {'R': 7, 'I': 3, 'S': 3, 'Y': 4, 'P': 5, 'L': 5, 'K': 9, 'D': 6, 'A': 3, 'T': 3, 'G': 5, 'N': 6, 'M': 4, 'W': 2, 'H': 9, 'V': 6, 'Q': 2, 'E': 5, 'F': 5, 'C': 5}], ['ACC', {'R': 5, 'I': 3, 'S': 3, 'Y': 5, 'P': 8, 'L': 2, 'K': 8, 'D': 6, 'A': 4, 'T': 3, 'G': 2, 'N': 5, 'M': 8, 'W': 1, 'H': 6, 'V': 4, 'Q': 5, 'E': 5, 'F': 4, 'C': 10}], ['ACG', {'R': 4, 'I': 7, 'S': 6, 'Y': 5, 'P': 3, 'L': 3, 'K': 4, 'D': 7, 'A': 6, 'T': 4, 'G': 6, 'N': 5, 'M': 7, 'W': 4, 'H': 2, 'V': 7, 'Q': 3, 'E': 2, 'F': 5, 'C': 7}], ['ACU', {'R': 12, 'I': 4, 'S': 6, 'Y': 4, 'P': 6, 'L': 5, 'K': 3, 'D': 5, 'A': 4, 'T': 3, 'G': 2, 'N': 5, 'M': 4, 'W': 6, 'H': 6, 'V': 8, 'Q': 1, 'E': 6, 'F': 5, 'C': 2}], ['AGA', {'R': 4, 'I': 4, 'S': 3, 'Y': 3, 'P': 1, 'L': 7, 'K': 1, 'D': 6, 'A': 5, 'T': 6, 'G': 5, 'N': 7, 'M': 7, 'W': 7, 'H': 2, 'V': 5, 'Q': 7, 'E': 5, 'F': 6, 'C': 6}], ['AGC', {'R': 9, 'I': 2, 'S': 3, 'Y': 6, 'P': 1, 'L': 6, 'K': 3, 'D': 2, 'A': 5, 'T': 5, 'G': 3, 'N': 5, 'M': 10, 'W': 7, 'H': 4, 'V': 8, 'Q': 6, 'E': 3, 'F': 7, 'C': 2}], ['AGG', {'R': 5, 'I': 5, 'S': 7, 'Y': 6, 'P': 5, 'L': 4, 'K': 4, 'D': 5, 'A': 3, 'T': 1, 'G': 4, 'N': 7, 'M': 3, 'W': 4, 'H': 4, 'V': 3, 'Q': 4, 'E': 5, 'F': 7, 'C': 11}], ['AGU', {'R': 5, 'I': 10, 'S': 4, 'Y': 5, 'P': 6, 'L': 5, 'K': 6, 'D': 4, 'A': 4, 'T': 4, 'G': 7, 'N': 5, 'M': 3, 'W': 1, 'H': 3, 'V': 3, 'Q': 6, 'E': 7, 'F': 3, 'C': 6}], ['AUA', {'R': 5, 'I': 5, 'S': 4, 'Y': 4, 'P': 5, 'L': 5, 'K': 1, 'D': 4, 'A': 7, 'T': 3, 'G': 8, 'N': 6, 'M': 3, 'W': 6, 'H': 6, 'V': 6, 'Q': 4, 'E': 6, 'F': 7, 'C': 2}], ['AUC', {'R': 4, 'I': 2, 'S': 5, 'Y': 8, 'P': 9, 'L': 6, 'K': 4, 'D': 5, 'A': 4, 'T': 3, 'G': 4, 'N': 8, 'M': 2, 'W': 4, 'H': 6, 'V': 4, 'Q': 7, 'E': 1, 'F': 1, 'C': 10}], ['AUG', {'R': 3, 'I': 7, 'S': 6, 'Y': 6, 'P': 1, 'L': 5, 'K': 6, 'D': 1, 'A': 3, 'T': 4, 'G': 10, 'N': 3, 'M': 4, 'W': 7, 'H': 5, 'V': 6, 'Q': 7, 'E': 7, 'F': 2, 'C': 4}], ['AUU', {'R': 4, 'I': 3, 'S': 2, 'Y': 5, 'P': 5, 'L': 5, 'K': 7, 'D': 4, 'A': 10, 'T': 2, 'G': 3, 'N': 7, 'M': 3, 'W': 7, 'H': 7, 'V': 5, 'Q': 4, 'E': 4, 'F': 6, 'C': 4}], ['CAA', {'R': 6, 'I': 3, 'S': 2, 'Y': 4, 'P': 5, 'L': 10, 'K': 2, 'D': 7, 'A': 6, 'T': 7, 'G': 3, 'N': 4, 'M': 5, 'W': 5, 'H': 5, 'V': 3, 'Q': 8, 'E': 3, 'F': 5, 'C': 4}], ['CAC', {'R': 9, 'I': 4, 'S': 6, 'Y': 5, 'P': 3, 'L': 1, 'K': 1, 'D': 0, 'A': 12, 'T': 7, 'G': 4, 'N': 7, 'M': 2, 'W': 4, 'H': 4, 'V': 9, 'Q': 4, 'E': 8, 'F': 3, 'C': 4}], ['CAG', {'R': 3, 'I': 1, 'S': 6, 'Y': 3, 'P': 7, 'L': 7, 'K': 5, 'D': 5, 'A': 4, 'T': 2, 'G': 3, 'N': 3, 'M': 4, 'W': 6, 'H': 8, 'V': 6, 'Q': 9, 'E': 7, 'F': 5, 'C': 3}], ['CAU', {'R': 5, 'I': 6, 'S': 2, 'Y': 3, 'P': 7, 'L': 7, 'K': 6, 'D': 6, 'A': 1, 'T': 4, 'G': 11, 'N': 7, 'M': 4, 'W': 5, 'H': 4, 'V': 5, 'Q': 2, 'E': 8, 'F': 2, 'C': 2}], ['CCA', {'R': 5, 'I': 7, 'S': 1, 'Y': 2, 'P': 4, 'L': 4, 'K': 5, 'D': 7, 'A': 5, 'T': 7, 'G': 1, 'N': 4, 'M': 9, 'W': 8, 'H': 5, 'V': 6, 'Q': 3, 'E': 3, 'F': 6, 'C': 5}], ['CCC', {'R': 4, 'I': 3, 'S': 8, 'Y': 5, 'P': 4, 'L': 3, 'K': 4, 'D': 6, 'A': 5, 'T': 4, 'G': 4, 'N': 8, 'M': 1, 'W': 7, 'H': 4, 'V': 7, 'Q': 7, 'E': 5, 'F': 3, 'C': 5}], ['CCG', {'R': 3, 'I': 4, 'S': 7, 'Y': 2, 'P': 4, 'L': 3, 'K': 4, 'D': 3, 'A': 6, 'T': 5, 'G': 5, 'N': 3, 'M': 4, 'W': 5, 'H': 8, 'V': 3, 'Q': 5, 'E': 8, 'F': 7, 'C': 8}], ['CCU', {'R': 8, 'I': 3, 'S': 2, 'Y': 5, 'P': 4, 'L': 4, 'K': 2, 'D': 6, 'A': 5, 'T': 6, 'G': 8, 'N': 6, 'M': 4, 'W': 3, 'H': 5, 'V': 4, 'Q': 4, 'E': 7, 'F': 5, 'C': 6}], ['CGA', {'R': 5, 'I': 2, 'S': 6, 'Y': 6, 'P': 6, 'L': 3, 'K': 1, 'D': 5, 'A': 5, 'T': 7, 'G': 5, 'N': 7, 'M': 8, 'W': 4, 'H': 3, 'V': 4, 'Q': 6, 'E': 7, 'F': 5, 'C': 2}], ['CGC', {'R': 4, 'I': 3, 'S': 5, 'Y': 4, 'P': 6, 'L': 3, 'K': 5, 'D': 5, 'A': 6, 'T': 6, 'G': 7, 'N': 1, 'M': 3, 'W': 5, 'H': 3, 'V': 7, 'Q': 8, 'E': 7, 'F': 3, 'C': 6}], ['CGG', {'R': 2, 'I': 3, 'S': 3, 'Y': 6, 'P': 6, 'L': 5, 'K': 6, 'D': 2, 'A': 4, 'T': 4, 'G': 10, 'N': 2, 'M': 3, 'W': 6, 'H': 3, 'V': 3, 'Q': 4, 'E': 6, 'F': 13, 'C': 6}], ['CGU', {'R': 3, 'I': 5, 'S': 1, 'Y': 3, 'P': 9, 'L': 3, 'K': 6, 'D': 4, 'A': 5, 'T': 4, 'G': 4, 'N': 8, 'M': 7, 'W': 5, 'H': 3, 'V': 7, 'Q': 7, 'E': 4, 'F': 5, 'C': 4}], ['CUA', {'R': 7, 'I': 4, 'S': 2, 'Y': 5, 'P': 8, 'L': 6, 'K': 4, 'D': 1, 'A': 5, 'T': 4, 'G': 3, 'N': 3, 'M': 8, 'W': 6, 'H': 5, 'V': 5, 'Q': 5, 'E': 9, 'F': 3, 'C': 4}], ['CUC', {'R': 5, 'I': 3, 'S': 6, 'Y': 6, 'P': 2, 'L': 13, 'K': 2, 'D': 4, 'A': 3, 'T': 3, 'G': 5, 'N': 9, 'M': 2, 'W': 3, 'H': 4, 'V': 7, 'Q': 5, 'E': 2, 'F': 6, 'C': 7}], ['CUG', {'R': 5, 'I': 4, 'S': 5, 'Y': 6, 'P': 5, 'L': 2, 'K': 2, 'D': 5, 'A': 2, 'T': 3, 'G': 3, 'N': 6, 'M': 5, 'W': 9, 'H': 2, 'V': 6, 'Q': 5, 'E': 2, 'F': 7, 'C': 13}], ['CUU', {'R': 8, 'I': 4, 'S': 2, 'Y': 3, 'P': 5, 'L': 6, 'K': 8, 'D': 3, 'A': 5, 'T': 2, 'G': 8, 'N': 2, 'M': 4, 'W': 3, 'H': 6, 'V': 9, 'Q': 5, 'E': 2, 'F': 4, 'C': 8}], ['GAA', {'R': 2, 'I': 8, 'S': 6, 'Y': 5, 'P': 3, 'L': 9, 'K': 7, 'D': 3, 'A': 5, 'T': 8, 'G': 4, 'N': 3, 'M': 8, 'W': 3, 'H': 3, 'V': 4, 'Q': 3, 'E': 6, 'F': 4, 'C': 3}], ['GAC', {'R': 5, 'I': 4, 'S': 2, 'Y': 4, 'P': 7, 'L': 2, 'K': 3, 'D': 7, 'A': 8, 'T': 6, 'G': 9, 'N': 4, 'M': 4, 'W': 6, 'H': 5, 'V': 2, 'Q': 6, 'E': 3, 'F': 4, 'C': 6}], ['GAG', {'R': 6, 'I': 2, 'S': 9, 'Y': 3, 'P': 5, 'L': 9, 'K': 2, 'D': 6, 'A': 2, 'T': 4, 'G': 8, 'N': 6, 'M': 1, 'W': 3, 'H': 4, 'V': 8, 'Q': 5, 'E': 8, 'F': 5, 'C': 1}], ['GAU', {'R': 4, 'I': 4, 'S': 5, 'Y': 9, 'P': 6, 'L': 9, 'K': 4, 'D': 1, 'A': 7, 'T': 8, 'G': 5, 'N': 6, 'M': 7, 'W': 3, 'H': 6, 'V': 2, 'Q': 3, 'E': 2, 'F': 4, 'C': 2}], ['GCA', {'R': 9, 'I': 7, 'S': 2, 'Y': 4, 'P': 7, 'L': 4, 'K': 7, 'D': 7, 'A': 7, 'T': 3, 'G': 4, 'N': 2, 'M': 3, 'W': 4, 'H': 5, 'V': 3, 'Q': 4, 'E': 4, 'F': 6, 'C': 5}], ['GCC', {'R': 5, 'I': 2, 'S': 6, 'Y': 4, 'P': 5, 'L': 5, 'K': 11, 'D': 2, 'A': 5, 'T': 6, 'G': 4, 'N': 4, 'M': 4, 'W': 3, 'H': 8, 'V': 6, 'Q': 4, 'E': 3, 'F': 5, 'C': 5}], ['GCG', {'R': 4, 'I': 6, 'S': 8, 'Y': 7, 'P': 5, 'L': 3, 'K': 7, 'D': 5, 'A': 4, 'T': 7, 'G': 7, 'N': 3, 'M': 2, 'W': 4, 'H': 3, 'V': 5, 'Q': 3, 'E': 6, 'F': 5, 'C': 3}], ['GCU', {'R': 5, 'I': 9, 'S': 4, 'Y': 8, 'P': 2, 'L': 7, 'K': 6, 'D': 0, 'A': 9, 'T': 3, 'G': 6, 'N': 3, 'M': 3, 'W': 6, 'H': 6, 'V': 4, 'Q': 3, 'E': 1, 'F': 3, 'C': 9}], ['GGA', {'R': 3, 'I': 7, 'S': 5, 'Y': 4, 'P': 4, 'L': 10, 'K': 5, 'D': 5, 'A': 7, 'T': 7, 'G': 3, 'N': 3, 'M': 3, 'W': 6, 'H': 4, 'V': 3, 'Q': 7, 'E': 2, 'F': 6, 'C': 3}], ['GGC', {'R': 2, 'I': 5, 'S': 5, 'Y': 3, 'P': 3, 'L': 3, 'K': 5, 'D': 8, 'A': 4, 'T': 11, 'G': 8, 'N': 6, 'M': 6, 'W': 2, 'H': 3, 'V': 5, 'Q': 2, 'E': 5, 'F': 8, 'C': 3}], ['GGG', {'R': 7, 'I': 3, 'S': 10, 'Y': 6, 'P': 6, 'L': 6, 'K': 3, 'D': 10, 'A': 2, 'T': 5, 'G': 2, 'N': 5, 'M': 2, 'W': 3, 'H': 5, 'V': 3, 'Q': 10, 'E': 3, 'F': 1, 'C': 5}], ['GGU', {'R': 6, 'I': 8, 'S': 5, 'Y': 4, 'P': 5, 'L': 8, 'K': 2, 'D': 4, 'A': 2, 'T': 6, 'G': 9, 'N': 8, 'M': 3, 'W': 2, 'H': 5, 'V': 1, 'Q': 5, 'E': 5, 'F': 6, 'C': 3}], ['GUA', {'R': 5, 'I': 4, 'S': 3, 'Y': 4, 'P': 6, 'L': 7, 'K': 2, 'D': 5, 'A': 6, 'T': 5, 'G': 2, 'N': 11, 'M': 3, 'W': 6, 'H': 5, 'V': 4, 'Q': 6, 'E': 5, 'F': 4, 'C': 4}], ['GUC', {'R': 2, 'I': 3, 'S': 5, 'Y': 6, 'P': 6, 'L': 4, 'K': 4, 'D': 4, 'A': 4, 'T': 4, 'G': 1, 'N': 7, 'M': 3, 'W': 4, 'H': 7, 'V': 9, 'Q': 8, 'E': 4, 'F': 8, 'C': 4}], ['GUG', {'R': 5, 'I': 7, 'S': 5, 'Y': 5, 'P': 3, 'L': 3, 'K': 3, 'D': 7, 'A': 4, 'T': 3, 'G': 3, 'N': 5, 'M': 10, 'W': 3, 'H': 3, 'V': 7, 'Q': 3, 'E': 8, 'F': 5, 'C': 5}], ['GUU', {'R': 3, 'I': 0, 'S': 2, 'Y': 4, 'P': 7, 'L': 6, 'K': 7, 'D': 6, 'A': 7, 'T': 3, 'G': 5, 'N': 8, 'M': 7, 'W': 4, 'H': 3, 'V': 5, 'Q': 6, 'E': 6, 'F': 2, 'C': 6}], ['UAA', {'R': 0, 'I': 0, 'S': 0, 'Y': 0, 'P': 0, 'L': 0, 'K': 0, 'D': 0, 'A': 0, 'T': 0, 'G': 0, 'N': 0, 'M': 0, 'W': 0, 'H': 0, 'V': 0, 'Q': 0, 'E': 0, 'F': 0, 'C': 0}], ['UAC', {'R': 4, 'I': 2, 'S': 5, 'Y': 4, 'P': 4, 'L': 2, 'K': 2, 'D': 6, 'A': 4, 'T': 6, 'G': 4, 'N': 9, 'M': 4, 'W': 10, 'H': 5, 'V': 5, 'Q': 3, 'E': 5, 'F': 5, 'C': 8}], ['UAG', {'R': 0, 'I': 0, 'S': 0, 'Y': 0, 'P': 0, 'L': 0, 'K': 0, 'D': 0, 'A': 0, 'T': 0, 'G': 0, 'N': 0, 'M': 0, 'W': 0, 'H': 0, 'V': 0, 'Q': 0, 'E': 0, 'F': 0, 'C': 0}], ['UAU', {'R': 7, 'I': 4, 'S': 3, 'Y': 3, 'P': 7, 'L': 5, 'K': 7, 'D': 6, 'A': 3, 'T': 6, 'G': 4, 'N': 3, 'M': 4, 'W': 4, 'H': 9, 'V': 5, 'Q': 6, 'E': 4, 'F': 1, 'C': 6}], ['UCA', {'R': 6, 'I': 2, 'S': 2, 'Y': 3, 'P': 5, 'L': 7, 'K': 4, 'D': 6, 'A': 2, 'T': 8, 'G': 8, 'N': 5, 'M': 4, 'W': 8, 'H': 3, 'V': 4, 'Q': 4, 'E': 4, 'F': 7, 'C': 5}], ['UCC', {'R': 3, 'I': 2, 'S': 7, 'Y': 5, 'P': 2, 'L': 4, 'K': 4, 'D': 6, 'A': 0, 'T': 10, 'G': 5, 'N': 9, 'M': 4, 'W': 4, 'H': 7, 'V': 5, 'Q': 4, 'E': 4, 'F': 3, 'C': 9}], ['UCG', {'R': 8, 'I': 2, 'S': 6, 'Y': 4, 'P': 8, 'L': 4, 'K': 7, 'D': 4, 'A': 2, 'T': 5, 'G': 3, 'N': 3, 'M': 1, 'W': 9, 'H': 2, 'V': 7, 'Q': 4, 'E': 6, 'F': 5, 'C': 7}], ['UCU', {'R': 4, 'I': 5, 'S': 6, 'Y': 4, 'P': 5, 'L': 3, 'K': 5, 'D': 8, 'A': 5, 'T': 6, 'G': 4, 'N': 8, 'M': 6, 'W': 6, 'H': 4, 'V': 3, 'Q': 6, 'E': 2, 'F': 4, 'C': 3}], ['UGA', {'R': 0, 'I': 0, 'S': 0, 'Y': 0, 'P': 0, 'L': 0, 'K': 0, 'D': 0, 'A': 0, 'T': 0, 'G': 0, 'N': 0, 'M': 0, 'W': 0, 'H': 0, 'V': 0, 'Q': 0, 'E': 0, 'F': 0, 'C': 0}], ['UGC', {'R': 2, 'I': 3, 'S': 6, 'Y': 2, 'P': 4, 'L': 8, 'K': 6, 'D': 3, 'A': 4, 'T': 4, 'G': 5, 'N': 12, 'M': 5, 'W': 6, 'H': 6, 'V': 2, 'Q': 4, 'E': 5, 'F': 5, 'C': 5}], ['UGG', {'R': 3, 'I': 6, 'S': 6, 'Y': 8, 'P': 6, 'L': 6, 'K': 2, 'D': 10, 'A': 2, 'T': 5, 'G': 2, 'N': 1, 'M': 5, 'W': 5, 'H': 3, 'V': 7, 'Q': 7, 'E': 4, 'F': 5, 'C': 4}], ['UGU', {'R': 5, 'I': 5, 'S': 6, 'Y': 5, 'P': 3, 'L': 4, 'K': 5, 'D': 6, 'A': 4, 'T': 10, 'G': 6, 'N': 4, 'M': 8, 'W': 4, 'H': 3, 'V': 7, 'Q': 6, 'E': 2, 'F': 2, 'C': 2}], ['UUA', {'R': 4, 'I': 4, 'S': 4, 'Y': 6, 'P': 2, 'L': 3, 'K': 8, 'D': 2, 'A': 2, 'T': 9, 'G': 3, 'N': 9, 'M': 5, 'W': 7, 'H': 6, 'V': 5, 'Q': 6, 'E': 5, 'F': 4, 'C': 3}], ['UUC', {'R': 1, 'I': 6, 'S': 4, 'Y': 8, 'P': 4, 'L': 5, 'K': 7, 'D': 2, 'A': 8, 'T': 5, 'G': 7, 'N': 7, 'M': 1, 'W': 2, 'H': 4, 'V': 9, 'Q': 2, 'E': 3, 'F': 6, 'C': 6}], ['UUG', {'R': 3, 'I': 3, 'S': 2, 'Y': 5, 'P': 4, 'L': 6, 'K': 6, 'D': 7, 'A': 3, 'T': 4, 'G': 1, 'N': 8, 'M': 6, 'W': 4, 'H': 7, 'V': 7, 'Q': 7, 'E': 5, 'F': 2, 'C': 7}], ['UUU', {'R': 3, 'I': 4, 'S': 4, 'Y': 6, 'P': 6, 'L': 6, 'K': 5, 'D': 3, 'A': 6, 'T': 6, 'G': 4, 'N': 3, 'M': 5, 'W': 8, 'H': 2, 'V': 6, 'Q': 6, 'E': 7, 'F': 5, 'C': 2}]]
I'm not sure this is answering the question but in general pandas is a great tool for this.
import pandas as pd
df = pd.DataFrame(dict(x[0])
This gives you a convenient table.
AAA AAC AAG AAU
A 9 6 6 4
C 5 5 7 7
D 9 5 5 3
E 4 5 5 7
Then you can sum over the columns if you like.
df.sum(axis=1)
resulting in
A 290
C 314
D 295
E 292
F 289
G 295
...
Or if you would rather create a bar chart for each codon you can do e.g.
df.AAA.plot(kind='bar')

Adding value depending on dictionary value

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

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