2

Hey guys I have a exam next week, and I need to get better with dictionaries. I have this code and I cant figure out how to go about it. Our class are writing code with python language. version 3 (Python 3)

Like say I have a dictionary, and I want to return true if that dictionary has like two or more keys that refer to the same value and false otherwise.

>>> duplicate({'a': 9, 'b': '9', 'c': 7, 9: True})
False
>>> duplicate({'a': 9, 'b': 9, 'c': 7})
True
'''

I gave it a shot but I dont think its right.

def duplicate(dict1):
    for a key, value in dict1.items():
       dict1[value] = key
    return dict1

like I dont get how to find those two keys VALUES.

please help out I would really appreciate it. Any hint or any solution would be better.

4

3 回答 3

0

很简单。

def duplicate(A): return len(A) != len(set(A.values()))
于 2013-07-25T22:55:56.533 回答
0

又快又脏:

def has_dupes(x):
    z = list()
    for val in x.values():
        if val in z:
            return True
        z.append(val)
    return False

高温高压

于 2013-07-25T22:51:57.927 回答
0
def duplicate(x):
    return len(x.values()) > len(set(x.values()))
于 2013-07-25T22:58:49.220 回答