简体   繁体   中英

List comprehension to replace values in a string with key strings from a dictionary

Asking for a bit of assistance. This function replaces the abbreviated words with standard spelling. I'm trying to replace lines 3-6 with a list comprehension. I tried this:

words[i] = [key for i, word in enumerate(words) for key, value in abbrevs.items()
            if word == abbrevs[key]] 

def text_deciphered(text_message, abbrevs):
    words = text_message.split()
    for i, word in enumerate(words):
        for key in abbrevs.keys():
            if word == abbrevs[key]:
                words[i] = key    
    words = ' '.join(words)
    return words

text_message = "Hey, wat r our plans for tn"
abbrevs = {"what": "wat", "are": "r", "tonight": "tn"}
print(text_deciphered(text_message, abbrevs))

But it merely returns the keys in abbrevs - what are tonight

First of all, the idea behind dictionaries is to lookup a value based on a key, not the other way around. This guarantees instant lookup instead of dumping out all of the entries and looking through them one by one as you're doing. Start by inverting your dict: abbrevs = {v: k for k, v in abbrevs.items()} or build it correctly from the start before proceeding.

After that adjustment, this can be a simple one-liner using a list comprehension and dict.get(element, element) . If element is not in the dictionary, the fallback value is element itself:

>>> text_message = "Hey, wat r our plans for tn"
>>> abbrevs = {"wat": "what", "r": "are", "tn": "tonight"}
>>> " ".join([abbrevs.get(x, x) for x in text_message.split()])
'Hey, what are our plans for tonight'

So your function is:

def text_deciphered(text_message, abbrevs):
    return " ".join([abbrevs.get(x, x) for x in text_message.split()])

Note that split is equivalent to \\s+ , so you may wind up squeezing your whitespace inadvertently. I'd prefer to use re.sub with a lambda to apply to every word:

>>> import re
>>> text_message = "Hey,   wat   r our plans for tn"
>>> abbrevs = {"wat": "what", "r": "are", "tn": "tonight"}
>>> re.sub(r"(\w+)", lambda x: abbrevs.get(x.group(), x.group()), text_message)
'Hey,   what   are our plans for tonight'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM