[英]Accessing a dict, replacing items in a user input with the information from the dict
Im trying to access a dict ive created called Brands. 我试图访问一个名为Brands的命令。 I am then trying to replace information from a user input with information from the dict.
然后,我尝试用dict中的信息替换用户输入中的信息。 I am struggiling to print out and replace text of the user input.
我正在努力打印并替换用户输入的文本。 Currently the only thing I can get it to do is print out all of the table content, with words inserted which unfortunately doesnt help very much.
目前,我唯一能做的就是打印出所有表内容,但插入的单词很遗憾没有太大帮助。
BRANDS = {
'Velcro': 'hook and loop fastener',
'Kleenex': 'tissues',
'Hoover': 'vacuum',
'Bandaid': 'sticking plaster',
'Thermos': 'vacuum flask',
'Dumpster': 'garbage bin',
'Rollerblade': 'inline skate',
'Asprin': 'acetylsalicylic acid'
}
userinput = input("Sentence: ")
print('a', userinput, 'is', BRANDS[userinput])
This is an example that my code must do. 这是我的代码必须执行的示例。
Sentence: I bought some Velcro shoes.
Result >>> I bought some hook and loop fastener shoes.
You can define a function to cycle through your dictionary and perform str.replace
repeatedly: 您可以定义一个函数来循环浏览字典并重复执行
str.replace
:
def replacer(x, d):
for k, v in d.items():
x = x.replace(k, v)
return x
userinput = input("Sentence: ")
print(replacer(userinput, BRANDS))
Example: 例:
Sentence: I bought some Velcro shoes.
I bought some hook and loop fastener shoes.
Try this: 尝试这个:
for key in BRANDS:
userinput = userinput.replace(key, BRANDS[key])
print(userinput)
OR: 要么:
def repl(d,s):
d={k:v for k,v in d.items() if k in s}
return s.replace(next(iter(d)),next(iter(d.values())))
inp = input("Sentence: ")
print(repl(BRANDS,inp))
Example Output: 示例输出:
Sentence: I bought some Velcro shoes
I bought some hook and loop fastener shoes
You can use dict.get()
with a list comprehension to iterate over the values of your user input (separated by space) and replace them with the values from your BRANDS dictionary. 您可以将
dict.get()
与列表dict.get()
一起使用,以迭代用户输入的值(以空格分隔),并将其替换为BRANDS词典中的值。 The dict.get()
method will first check if a key of that particular value of i
exists. dict.get()
方法将首先检查该i
特定值的键是否存在。 If it does it will return the corresponding value from the BRANDS dict or else just keep the original value of i
. 如果这样做,它将从BRANDS字典返回相应的值,否则只需保留
i
的原始值。 You can then pass this list to str.join()
to form the final string 然后,您可以将此列表传递给
str.join()
以形成最终字符串
print(' '.join(BRANDS.get(i,i) for i in userinput.split()))
Hope this helps! 希望这可以帮助!
Here is an example that does not require looping over all elements in your dict
. 这是一个示例,不需要遍历
dict
所有元素。
By using a regex, you can do the replacement in a single traversal instead of using str.replace
for each element in BRANDS
. 通过使用正则表达式,您可以在单个遍历中进行替换,而不是对
BRANDS
每个元素使用str.replace
。
import re
BRANDS = ...
def replacer(sentence, replacements):
pattern = '|'.join(replacements)
repl = lambda m: replacements[m.group()]
return re.sub(pattern, repl, sentence)
userinput = input("Sentence: ")
print(replacer(userinput, BRANDS))
Sentence: Kleenex and Hoover
tissues and vacuum
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.