【问题标题】:Using Python dictionary values to return key as result使用 Python 字典值将键作为结果返回
【发布时间】:2018-05-15 12:21:07
【问题描述】:

我不确定为什么此代码在一些输入中失败(来自 CodingBat,问题链接:Exercise Link)。问题详情如下,我可以用if elif 语句来做这个问题,但我想使用字典。另外,我读到不建议从字典中获取键值,如下所示。但是,如果可以指出以下程序中的问题,我将不胜感激。

你开得太快了,警察拦住了你。编写代码来计算结果,编码为 int 值:0=无票,1=小票,2=大票。如果速度小于等于 60,则结果为 0。如果速度介于 61 和 80 之间,则结果为 1。如果速度大于或等于 81,则结果为 2。除非是你的生日——那天,你的在所有情况下,速度都可以提高 5 倍。

  • caught_speeding(60, False) → 0
  • caught_speeding(65, False) → 1
  • caught_speeding(65, True) → 0
def caught_speeding(speed, is_birthday):
    Bir_dict = {0:speed<=60,1:61<=speed<=80,2:speed>=81}
    NoBir_dict = {0:speed<=65,1:66<=speed<=85,2:speed>=86}
    def getKey(dict,value):
        return [key for key in dict.keys() if (dict[key] == value)]
    if is_birthday:
        out1=getKey(Bir_dict,True)
        return out1[0]
    else:
        out2=getKey(NoBir_dict,True)
        return out2[0]

程序失败了

caught_speeding(65, False)
caught_speeding(65, True)

并为

工作
caught_speeding(70, False)
caught_speeding(75, False)
caught_speeding(75, True)
caught_speeding(40, False)
caught_speeding(40, True)
caught_speeding(90, False)
caught_speeding(60, False)
caught_speeding(80, False)

【问题讨论】:

    标签: python dictionary


    【解决方案1】:

    看起来你混合了Bir_dict 和NoBir_dict。你可以试试下面的代码吗?

    def caught_speeding(speed, is_birthday):
            Bir_dict = {0:speed<=65,1:66<=speed<=85,2:speed>=86}
            NoBir_dict = {0:speed<=60,1:61<=speed<=80,2:speed>=81}
            def getKey(dict,value):
                return [key for key in dict.keys() if (dict[key] == value)]
            if is_birthday:
                out1=getKey(Bir_dict,True)
                return out1[0]
            else:
                out2=getKey(NoBir_dict,True)
                return out2[0]
    

    尽管它有效,但我可以建议使用字典的另一种方法:票证定义不会相互干扰,换句话说,字典中只能有一个 True 语句。因此,您可以将代码修改为:

    def caught_speeding(speed, is_birthday):
            Bir_dict = {0:speed<=65,1:66<=speed<=85,2:speed>=86}
            NoBir_dict = {0:speed<=60,1:61<=speed<=80,2:speed>=81}
            def getKey(dict):
                return [key for key in dict.keys() if (dict[key] == True)]
            if is_birthday:
                out1=getKey(Bir_dict)
                return out1[0]
            else:
                out2=getKey(NoBir_dict)
                return out2[0]
    

    【讨论】:

    • 咩!谢谢你。我应该休息一下。
    • 欣赏改进。
    【解决方案2】:

    这个问题不会认为生日字典有更高的可用值吗?

    如果今天是我的生日,而我即将 65 岁: 我希望没有票。 但是,如果您生成字典并打印它们,您会发现情况并非如此:

    def caught_speeding(speed, is_birthday):
        Bir_dict = {0:speed<=60,1:61<=speed<=80,2:speed>=81}
        print Bir_dict
        NoBir_dict = {0:speed<=65,1:66<=speed<=85,2:speed>=86}
        print NoBir_dict
        def getKey(dict,value):
            return [key for key in dict.keys() if (dict[key] == value)]
        if is_birthday:
            out1=getKey(Bir_dict,True)
            return out1[0]
        else:
            out2=getKey(NoBir_dict,True)
            return out2[0]
    

    输出:

    {0: False, 1: True, 2: False}
    {0: True, 1: False, 2: False}
    0
    {0: False, 1: True, 2: False}
    {0: True, 1: False, 2: False}
    1
    

    您只需在字典对象中交叉值

    【讨论】:

      猜你喜欢
      • 2021-05-19
      • 1970-01-01
      • 2014-08-26
      • 2013-05-25
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      相关资源
      最近更新 更多