【问题标题】:How do I find if a value exists in a dictionary? [duplicate]如何查找字典中是否存在值? [复制]
【发布时间】:2022-01-12 19:28:44
【问题描述】:

如果我有字典:

d = {0:'Fred', 1:'Fred', 2:'Zippy', 3:'Bob'}

我怎样才能知道"Fred"在其中出现了多少次?

【问题讨论】:

    标签: python dictionary key


    【解决方案1】:

    你可以这样做:

    c=0
    for i in d.values():
        if i=='Fred':
            c+=1
    print(c)
    

    【讨论】:

    • 通常不鼓励回答明显的重复问题,这可能会引起反对意见。
    • 感谢您的建议。我是新来的,从现在开始我会小心的。
    猜你喜欢
    • 1970-01-01
    • 2011-04-20
    • 1970-01-01
    • 2021-11-02
    • 2019-10-12
    • 2012-01-03
    • 1970-01-01
    • 2021-11-30
    • 2012-03-27
    相关资源
    最近更新 更多