匹配多个键的打印值 [英] print value for matching multiple key

查看:50
本文介绍了匹配多个键的打印值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个键映射到一个值.如果有任何键匹配,则打印相应的值.这只是我要执行的要旨,而不是实际的代码.

I have multiple keys mapped to a single value. If any key matches then print the corresponding value. This is only the gist of what I want to do and not the actual code.

responses = {
    ("hi","Yo"):"Lets get started."
}

def get_response(user_message):
    if user_message in responses:
        user_message = responses[user_message]
    print(user_message)

get_response("hi") #should print 'Lets get started'
get_response("Yo") #should print 'Lets get started'

这也是将多个键存储为单个值的正确方法.

Also is this the right way of storing multiple keys to a single value.

推荐答案

要完成您要执行的操作,您必须检查是否"hi"是否正确.是 in 中的元组.(("hi","Yo")是一个元组).

To accomplish what you're trying to do, you would have to check whether "hi" is in the tuple. (("hi", "Yo") is a tuple).

因此您可以执行以下操作:

So you can do something like this:

for key in responses:
    if user_message in key:
        print(responses[key])

这篇关于匹配多个键的打印值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆