Reputation: 11
For example, I have a dict and I want to use the string "apple"
and return the corresponding values in the dictionary of every given letter in the string:
'dot-dash-dot-dash-dash-dot-dot-dash-dash-dot-dot-dash-dot-dot-dot'
morse_dict = {
'a': 'dot-dash',
'b': 'dash-dot-dot-dot',
'c': 'dash-dot-dash-dot',
'd': 'dash-dot-dot',
'e': 'dot',
'f': 'dot-dot-dash-dot',
'g': 'dash-dash-dot',
'h': 'dot-dot-dot-dot',
'i': 'dot-dot',
'j': 'dot-dash-dash-dash',
'k': 'dash-dot-dash',
'l': 'dot-dash-dot-dot',
'm': 'dash-dash',
'n': 'dash-dot',
'o': 'dash-dash-dash',
'p': 'dot-dash-dash-dot',
'q': 'dash-dash-dot-dash',
'r': 'dot-dash-dot',
's': 'dot-dot-dot',
't': 'dash',
'u': 'dot-dot-dash',
'v': 'dot-dot-dot-dash',
'w': 'dot-dash-dash',
'y': 'dash-dot-dash-dash',
'z': 'dash-dash-dot-dot'
}
a = tuple("word")
for x in a:
return("-".join([val for keys, val in morse_dict.items() if x in keys]))
What am I doing wrong here?
Upvotes: 1
Views: 162
Reputation: 151
Try the below simple code with your given dictionary which is fulfilling your written requirement,
a = "word"
fin = []
for x in a.lower():
if x in morse_dict.keys():
fin.append(morse_dict[x])
print('-'.join(fin))
** Your can replace print
with return
as per your requirement in a function
Upvotes: 0
Reputation: 23140
I'm not sure how to explain what exactly you did wrong, because you seem to have made this unnecessarily complicated.
The answer to the title question,
how to return the value of a key if a given string matches the key
is to use the fundamental feature of a dictionary, a key lookup:
>>> morse_dict = {'a': 'dot-dash', ...}
>>> morse_dict['a']
'dot-dash'
This will raise a KeyError
exception if a key is not contained in the dictionary. To avoid this, you have two options:
.get
method instead of the []
lookup, which allows to specify a default value which is returned in case the key does not exist.I think in your case a default value would not be helpful, because what would be the morse code for an unknown letter? And if you choose to use an empty string ''
as default value, you would end up with a result containing --
.
So, when using the first option, what you want to do can simply be written as:
'-'.join(morse_dict[letter] for letter in word if letter in morse_dict)
i.e., for each letter in the word, look up the corresponding morse code (which already consists of dash
or dot
joined by -
), and join them all by -
, and ignore letters for which there is no morse code.
Upvotes: 3
Reputation: 437
You can simply use a list comprehension like this :
a = "word"
morse_a = "-".join([morse_dict[letter] for letter in a])
Otherwise, I saw that you test the characters to determine if they are in morse_dict
. A pythonic way to plan for unplanned characters could be with the method dict.get()
:
a = "word"
morse_letters = [morse_dict.get(letter, None)] # Get dict values
morse_letters = tuple(filter(None, morse_letters)) # Filter None and cast into a tuple
morse_a = "-".join(morse_letters) # Join everything
Upvotes: 1