search

Home  >  Q&A  >  body text

dict - Python determines whether there are duplicate values ​​in a dictionary

The input is a dictionary, for example: {'A': 1, 'B': 1, 'C': 3}, how to determine whether there are duplicate values ​​in the dictionary(values)?

扔个三星炸死你扔个三星炸死你2752 days ago1364

reply all(2)I'll reply

  • 扔个三星炸死你

    扔个三星炸死你2017-06-28 09:26:49

    Reference article: Python’s list, dict, json and other common operations

    Check whether there are duplicate values ​​in the dictionary

    >>> def has_duplicates(d):
            return len(d) != len(set(d.values()))
    >>> print has_duplicates({'A': 1, 'B': 1, 'C': 3})
    True
    >>>

    reply
    0
  • 阿神

    阿神2017-06-28 09:26:49

    d = {'A': 1, 'B': 1, 'C': 3}
    
    #不相等即有重复值
    print len(d.values()) == len(set(d.values()))

    reply
    0
  • Cancelreply