【问题标题】:how to check if a string is in a value in dictionary python [duplicate]如何检查字符串是否在字典python中的值中[重复]
【发布时间】:2021-04-11 01:32:06
【问题描述】:

fruit={'apple':'one','banana':'two','orange':'three'}
a='one'
if a in fruit.value():
    print(a,"was found!")

当我在 python 上运行它时,它显示: AttributeError: 'dict' 对象没有属性 'value'

【问题讨论】:

    标签: python


    【解决方案1】:

    字典没有称为 key() 的元素。您打算使用的是以 s 结尾的 keys() 。它应该是这样的:

    fruit={'apple':'one','banana':'two','orange':'three'}
    a='apple'
    
    if a in fruit.keys():
        print(f"Yes, key: '{a}' exists in dictionary")
    

    【讨论】:

      【解决方案2】:

      只需像这样检查它:

      fruit={'apple':'one','banana':'two','orange':'three'}
      a='apple'
      if a in fruit:
          print(a,"was found!")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-22
        • 2013-01-30
        • 2016-11-21
        • 2021-10-04
        • 1970-01-01
        • 2019-03-01
        相关资源
        最近更新 更多