Kelvin Davis Kelvin Davis - 1 year ago 94
Python Question

Parsing Through Dictionary with mutiple keys correctly

Yes, I ask a lot of questions. So I've wrote the function to parse through my created Dictionary of abbreviations and meanings. But It wont work for longer abbreviations. I think its finding the first instance of piece and spitting that out. I want it to take the whole input and return the value that responds to that. Single letters and double letters work mostly but longer stuff is a no go.

Example of my key:values
{'b00n': ['new person'], 'hv': ['have'], 'wuwtb': ['what do you want to talk about'], 'l8rz': ['later'], 'jhm': ['just hold me'],

def main():
game = True
myDict = CreateDictionary('textToEnglish.csv')
while game == True:
abbrev = input("Please enter text abbreviations seperated by comma:")
newList = list(abbrev)
print([v for k, v in myDict.items() if k in newList])

answer = input("Would you like to input more abbreviations? yes(y) or no(n):")
if answer == "y":
game = True
game = False

Answer Source

abbrev is a string, when you turn it into a list, you're getting a list of each letter:

>>> abbrev = 'one, two, three'
>>> list(abbrev)
['o', 'n', 'e', ',', ' ', 't', 'w', 'o', ',', ' ', 't', 'h', 'r', 'e', 'e']

You may want something like this instead:

>>> abbrev.split(',')
['one', ' two', ' three']
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download