Replace dictionary values with a list - python

A dictionary like this:
{' ': None, 'a': None, 'b': None, 'c': None, 'd': None, 'e': None, 'f': None, 'g': None, 'h': None, 'i': None, 'j': None, 'k': None, 'l': None, 'm': None, 'n': None, 'o': None, 'p': None, 'q': None, 'r': None, 's': None, 't': None, 'u': None, 'v': None, 'w': None, 'x': None, 'y': None, 'z': None}
And a
list = [0,1,2,....26]
And I want to replace all values None with values from a list.
So that we can have a dict
{'': 0, 'a' :1....'z': 26}

You can use a loop or dictionary-comprehension like this:
d = {' ': None, 'a': None, 'b': None, 'c': None, 'd': None, 'e': None, 'f': None, 'g': None, 'h': None, 'i': None, 'j': None, 'k': None, 'l': None, 'm': None, 'n': None, 'o': None, 'p': None, 'q': None, 'r': None, 's': None, 't': None, 'u': None, 'v': None, 'w': None, 'x': None, 'y': None, 'z': None}
l = [x for x in range(len(d.keys()))]
d2 = { k:l[i] for i, k in enumerate(d.keys()) }
print(d2)
# Outputs:
# {' ': 0, 'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10, 'k': 11, 'l': 12, 'm': 13, 'n': 14, 'o': 15, 'p': 16, 'q': 17, 'r': 18, 's': 19, 't': 20, 'u': 21, 'v': 22, 'w': 23, 'x': 24, 'y': 25, 'z': 26}

Related

Map Lowercase Alphabet to Integers [duplicate]

This question already has answers here:
creating dict where keys are alphabet letters and values are 1-26 using dict comprehension
(9 answers)
Closed 2 months ago.
i cant make a dict where to every one letter will be a value in the n + 1 format, start from 1.
this is my code:
dict1 = {x: y for x in 'abcdefghijklmnopqrstunvwxyz' for y in range(1, 20, 1)}
print(dict1)
i get it:
{'a': 19, 'b': 19, 'c': 19, 'd': 19, 'e': 19, 'f': 19, 'g': 19, 'h': 19, 'i': 19, 'j': 19, 'k': 19, 'l': 19, 'm': 19, 'n': 19, 'o': 19, 'p': 19, 'q': 19, 'r': 19, 's': 19, 't': 19, 'u': 19, 'v': 19, 'w': 19, 'y': 19, 'z': 19}
can u tell me why and how to get it: {'a': 1, 'b':2...
Use the tools provided by Python to make this easier to implement and read. First, don't hardcode the lowercase alphabet, use the stdlib string. Next, don't do the arithmetic yourself, it's easy to get wrong: there are 26 letters but range(1, 20, 1) generates only 19 digits. You also have a typo in your alphabet (I assume): tunvw. Lastly, use enumerate() which generates tuples consisting of an element of a sequence and an auto-incrementing integer, and use the start keyword argument to set the starting integer value.
import string
d = {x: y for x, y in enumerate(string.ascii_lowercase, start=1)}
Your code doesn't produce the result you want because for each integer from 1 to 19, it assigns the integer to the value for each key ('a', 'b', 'c', etc.). The first iteration of the outer for produces the dictionary {'a': 1, 'b': 2, 'c': 3, etc.}. Then, each subsequent iteration of the outer for overwrites these values with the new integer: 2, 3, 4, etc.
This is easy to see if we convert the comprehension to loops:
d = {}
for y in range(1, 20, 1):
for x in 'abcdefghijklmnopqrstuvwxyz':
d[x] = y
print(d)
{'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1, 'f': 1, 'g': 1, 'h': 1, 'i': 1, 'j': 1, 'k': 1, 'l': 1, 'm': 1, 'n': 1, 'o': 1, 'p': 1, 'q': 1, 'r': 1, 's': 1, 't': 1, 'u': 1, 'v': 1, 'w': 1, 'x': 1, 'y': 1, 'z': 1}
{'a': 2, 'b': 2, 'c': 2, 'd': 2, 'e': 2, 'f': 2, 'g': 2, 'h': 2, 'i': 2, 'j': 2, 'k': 2, 'l': 2, 'm': 2, 'n': 2, 'o': 2, 'p': 2, 'q': 2, 'r': 2, 's': 2, 't': 2, 'u': 2, 'v': 2, 'w': 2, 'x': 2, 'y': 2, 'z': 2}
{'a': 3, 'b': 3, 'c': 3, 'd': 3, 'e': 3, 'f': 3, 'g': 3, 'h': 3, 'i': 3, 'j': 3, 'k': 3, 'l': 3, 'm': 3, 'n': 3, 'o': 3, 'p': 3, 'q': 3, 'r': 3, 's': 3, 't': 3, 'u': 3, 'v': 3, 'w': 3, 'x': 3, 'y': 3, 'z': 3}
...
{'a': 19, 'b': 19, 'c': 19, 'd': 19, 'e': 19, 'f': 19, 'g': 19, 'h': 19, 'i': 19, 'j': 19, 'k': 19, 'l': 19, 'm': 19, 'n': 19, 'o': 19, 'p': 19, 'q': 19, 'r': 19, 's': 19, 't': 19, 'u': 19, 'v': 19, 'w': 19, 'x': 19, 'y': 19, 'z': 19}
edit: You don't need a comprehension to do this. There is another, pretty clean solution:
import string
d = dict(enumerate(string.ascii_lowercase, start=1))
This solution relies on the dict(iterable) constructor:
Help on class dict in module builtins:
class dict(object)
| dict() -> new empty dictionary
| dict(mapping) -> new dictionary initialized from a mapping object's
| (key, value) pairs
| dict(iterable) -> new dictionary initialized as if via:
| d = {}
| for k, v in iterable:
| d[k] = v
Try to zip the alphabet and the range to see what you get like that:
z1 = zip('abcdefghijklmnopqrstunvwxyz', range(1, 28, 1))
Which will give us:
for tup in z1:
print(tup)
Output:
('a', 1)
('b', 2)
('c', 3)
('d', 4)
('e', 5)
('f', 6)
('g', 7)
('h', 8)
('i', 9)
('j', 10)
('k', 11)
('l', 12)
('m', 13)
('n', 14)
('o', 15)
('p', 16)
('q', 17)
('r', 18)
('s', 19)
('t', 20)
('u', 21)
('n', 22)
('v', 23)
('w', 24)
('x', 25)
('y', 26)
('z', 27)
Now you can assign each element of each tuple to x, y:
d1 = {x, y for x, y in zip('abcdefghijklmnopqrstunvwxyz', range(1, 28, 1))}
Output:
print(d1)
{'a': 1,
'b': 2,
'c': 3,
'd': 4,
'e': 5,
'f': 6,
'g': 7,
'h': 8,
'i': 9,
'j': 10,
'k': 11,
'l': 12,
'm': 13,
'n': 22,
'o': 15,
'p': 16,
'q': 17,
'r': 18,
's': 19,
't': 20,
'u': 21,
'v': 23,
'w': 24,
'x': 25,
'y': 26,
'z': 27}

Websocket data converted into dictionary in python

i have two(n) websocket messages input like this, separated by "==new msg=="
(don't know why but some of the messages are longer..)
++Rcv raw: b'\x81~\x02\xff{"data":[{"c":null,"p":20926.99,"s":"BINANCE:BTCUSDT","t":1656017217026,"v":0.0008},{"c":null,"p":20927.45,"s":"BINANCE:BTCUSDT","t":1656017217056,"v":0.0005},{"c":null,"p":20930.51,"s":"BINANCE:BTCUSDT","t":1656017217079,"v":0.001},{"c":null,"p":20930.51,"s":"BINANCE:BTCUSDT","t":1656017217079,"v":0.00061},{"c":null,"p":20930.52,"s":"BINANCE:BTCUSDT","t":1656017217079,"v":0.1},{"c":null,"p":20931.18,"s":"BINANCE:BTCUSDT","t":1656017217079,"v":0.05098},{"c":null,"p":20930.5,"s":"BINANCE:BTCUSDT","t":1656017217201,"v":0.03},{"c":null,"p":20930.5,"s":"BINANCE:BTCUSDT","t":1656017217201,"v":0.03654},{"c":null,"p":20927.9,"s":"BINANCE:BTCUSDT","t":1656017217649,"v":0.03},{"c":null,"p":20927.9,"s":"BINANCE:BTCUSDT","t":1656017217649,"v":0.01882}],"type":"trade"}'
++Rcv decoded: fin=1 opcode=1 data=b'{"data":[{"c":null,"p":20926.99,"s":"BINANCE:BTCUSDT","t":1656017217026,"v":0.0008},{"c":null,"p":20927.45,"s":"BINANCE:BTCUSDT","t":1656017217056,"v":0.0005},{"c":null,"p":20930.51,"s":"BINANCE:BTCUSDT","t":1656017217079,"v":0.001},{"c":null,"p":20930.51,"s":"BINANCE:BTCUSDT","t":1656017217079,"v":0.00061},{"c":null,"p":20930.52,"s":"BINANCE:BTCUSDT","t":1656017217079,"v":0.1},{"c":null,"p":20931.18,"s":"BINANCE:BTCUSDT","t":1656017217079,"v":0.05098},{"c":null,"p":20930.5,"s":"BINANCE:BTCUSDT","t":1656017217201,"v":0.03},{"c":null,"p":20930.5,"s":"BINANCE:BTCUSDT","t":1656017217201,"v":0.03654},{"c":null,"p":20927.9,"s":"BINANCE:BTCUSDT","t":1656017217649,"v":0.03},{"c":null,"p":20927.9,"s":"BINANCE:BTCUSDT","t":1656017217649,"v":0.01882}],"type":"trade"}'
==new msg==
++Rcv raw: b'\x81~\x03\x03{"data":[{"c":null,"p":20927.9,"s":"BINANCE:BTCUSDT","t":1656017217649,"v":0.08477},{"c":null,"p":20927.89,"s":"BINANCE:BTCUSDT","t":1656017217678,"v":0.1},{"c":null,"p":20927.89,"s":"BINANCE:BTCUSDT","t":1656017217678,"v":0.25},{"c":null,"p":20927.89,"s":"BINANCE:BTCUSDT","t":1656017217678,"v":0.07053},{"c":null,"p":20927.89,"s":"BINANCE:BTCUSDT","t":1656017217694,"v":0.08207},{"c":null,"p":20926.98,"s":"BINANCE:BTCUSDT","t":1656017217698,"v":0.05098},{"c":null,"p":20926.31,"s":"BINANCE:BTCUSDT","t":1656017217698,"v":0.1},{"c":null,"p":20925.5,"s":"BINANCE:BTCUSDT","t":1656017217698,"v":0.16424},{"c":null,"p":20927.87,"s":"BINANCE:BTCUSDT","t":1656017217711,"v":0.00052},{"c":null,"p":20923.16,"s":"BINANCE:BTCUSDT","t":1656017217927,"v":0.0155}],"type":"trade"}'
++Rcv decoded: fin=1 opcode=1 data=b'{"data":[{"c":null,"p":20927.9,"s":"BINANCE:BTCUSDT","t":1656017217649,"v":0.08477},{"c":null,"p":20927.89,"s":"BINANCE:BTCUSDT","t":1656017217678,"v":0.1},{"c":null,"p":20927.89,"s":"BINANCE:BTCUSDT","t":1656017217678,"v":0.25},{"c":null,"p":20927.89,"s":"BINANCE:BTCUSDT","t":1656017217678,"v":0.07053},{"c":null,"p":20927.89,"s":"BINANCE:BTCUSDT","t":1656017217694,"v":0.08207},{"c":null,"p":20926.98,"s":"BINANCE:BTCUSDT","t":1656017217698,"v":0.05098},{"c":null,"p":20926.31,"s":"BINANCE:BTCUSDT","t":1656017217698,"v":0.1},{"c":null,"p":20925.5,"s":"BINANCE:BTCUSDT","t":1656017217698,"v":0.16424},{"c":null,"p":20927.87,"s":"BINANCE:BTCUSDT","t":1656017217711,"v":0.00052},{"c":null,"p":20923.16,"s":"BINANCE:BTCUSDT","t":1656017217927,"v":0.0155}],"type":"trade"}'
==new msg==
and i have some code so far
import websocket
import json
from collections import ChainMap
y = []
c = []
def on_message(ws, message):
print("==new msg==")
y.append(json.loads(message))
def on_error(ws, error):
print(error)
def on_close(ws,close_status_code, close_reason):
print("==closed==")
print(len(y))
c = dict(ChainMap(*y))
print(c)
def on_open(ws):
ws.send('{"type":"subscribe","symbol":"BINANCE:BTCUSDT"}')
if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp("wss://ws.finnhub.io?token=****ps2ad3iahju47mdg",
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
ws.run_forever()
the output is:
==closed==
2
{'data': [{'c': None, 'p': 20926.99, 's': 'BINANCE:BTCUSDT', 't': 1656017217026, 'v': 0.0008}, {'c': None, 'p': 20927.45, 's': 'BINANCE:BTCUSDT', 't': 1656017217056, 'v': 0.0005}, {'c': None, 'p': 20930.51, 's': 'BINANCE:BTCUSDT', 't': 1656017217079, 'v': 0.001}, {'c': None, 'p': 20930.51, 's': 'BINANCE:BTCUSDT', 't': 1656017217079, 'v': 0.00061}, {'c': None, 'p': 20930.52, 's': 'BINANCE:BTCUSDT', 't': 1656017217079, 'v': 0.1}, {'c': None, 'p': 20931.18, 's': 'BINANCE:BTCUSDT', 't': 1656017217079, 'v': 0.05098}, {'c': None, 'p': 20930.5, 's': 'BINANCE:BTCUSDT', 't': 1656017217201, 'v': 0.03}, {'c': None, 'p': 20930.5, 's': 'BINANCE:BTCUSDT', 't': 1656017217201, 'v': 0.03654}, {'c': None, 'p': 20927.9, 's': 'BINANCE:BTCUSDT', 't': 1656017217649, 'v': 0.03}, {'c': None, 'p': 20927.9, 's': 'BINANCE:BTCUSDT', 't': 1656017217649, 'v': 0.01882}], 'type': 'trade'}
only from the first message..?
The goal is to calculate the volume-weighted average price of trades made during a 1 min interval
output should look like this:
2022-05-04 12:22:33 price:20926.99 volume:0.0008
2022-05-04 12:23:43 price:20926.99 volume:0.0058
2022-05-04 12:24:53 price:20927.99 volume:0.0608

Get a value of key from JSON response in Python

I am accessing an API through a get request from which I get a response.
This is my code:
import requests
import json
symbol = "AAPL"
end_point="https://api.polygon.io/v2/snapshot/locale/us/markets/stocks/tickers/"+symbol+"?apiKey=my_key"
r=requests.get(end_point).json()
print(r)
print(r['min'])
print(r) returned:
{'request_id': '2735c0be51de7719fd99460fe8696080', 'status': 'OK', 'ticker': {'day': {'c': 172.93, 'h': 175.48, 'l': 172.37, 'o': 174.14, 'v': 65575561, 'vw': 174.0984}, 'lastQuote': {'P': 172.83, 'S': 3, 'p': 172.82, 's': 1, 't': 1644524332922450142}, 'lastTrade': {'c': None, 'i': '139592', 'p': 172.8199, 's': 2014, 't': 1644524331573573011, 'x': 4}, 'min': {'av': 65559987, 'c': 172.9, 'h': 173.14, 'l': 172.89, 'o': 173.09, 'v': 107429, 'vw': 173.0138}, 'prevDay': {'c': 176.28, 'h': 176.65, 'l': 174.9, 'o': 176.05, 'v': 71204538, 'vw': 175.8287}, 'ticker': 'AAPL', 'todaysChange': -3.46, 'todaysChangePerc': -1.963, 'updated': 1644524331573573011}}
but when I try to access the key "min", I get key error:
KeyError: 'min'
This is super-simple. What am I doing wrong?
Please try:
r['ticker']['min']
the key min is inside ticker. Do this:
r['ticker']['min']

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 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