-3

In order to create a function that will be neccesary to create another one later I am working with dict and keys. In this way I have been searching some information about how they work. But when I have to use dict and if statements I usually get stuck.

I am workin in a function that return the number of values in dict that are also keys in dict. My fisrt thought was to use a for loop but I get stuck in the if statement code. It seems be wrong but I don't know what could be. I have deduced that I must use an in operator and the variables k and d, and also an indexing but I don't know if I am using them properly. Any help will be useful. Thanks in advance

This is my current progress:

def count_values_that_are_keys(d):
'''(dict) -> int

Return the number of values in d that are also keys in d.

>>> count_values_that_are_keys({1: 2, 2: 3, 3: 3})
3
>>> count_values_that_are_keys({1: 1})
1
>>> count_values_that_are_keys({1: 2, 2: 3, 3: 0})
2
>>> count_values_that_are_keys({1: 2})
0
'''

result = 0
for k in d:
    if [d in [k]]: # This part it seems wrong cause I don't get what I expect
         result = result + 1

return result
hugo
  • 1
  • 1
  • Please indent Python code properly. Otherwise you are introducing new problems into the code that people are reading for you. – khelwood Feb 09 '17 at 11:35

3 Answers3

1

Sticking with something like your current approach, it's easier just to make a list of dictionary keys and then check membership of the dictionary values in that list. For large dictionaries you'd want to use dict_keys = set(d.keys()) for faster lookup.

def count_values_that_are_keys(d):
    '''(dict) -> int

    Return the number of values in d that are also keys in d.

    >>> count_values_that_are_keys({1: 2, 2: 3, 3: 3})
    3
    >>> count_values_that_are_keys({1: 1})
    1
    >>> count_values_that_are_keys({1: 2, 2: 3, 3: 0})
    2
    >>> count_values_that_are_keys({1: 2})
    0
    '''

    dict_keys = d.keys()    

    result = 0
    for key, value in d.items():
        if value in dict_keys:
            result += 1

    return result

print(count_values_that_are_keys({1: 2, 2: 3, 3: 3}))
print(count_values_that_are_keys({1: 1}))
print(count_values_that_are_keys({1: 2, 2: 3, 3: 0}))
print(count_values_that_are_keys({1: 2})) 
roganjosh
  • 12,594
  • 4
  • 29
  • 46
  • Oh I didn't know that method. Is there any method to write the if statement by using an in operator and the variables k and d, and also an indexing ? I mean is this possible? Just wondering. Thanks anyway. – hugo Feb 09 '17 at 12:00
  • 1
    @hugo I'm not really clear on what you're asking but you cannot index a dictionary because it has [no fixed order](http://stackoverflow.com/questions/15479928/why-is-the-order-in-dictionaries-and-sets-arbitrary). If you feel that either of these answers have solved your problem, please consider [marking them as accepted](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) so that the question is closed. – roganjosh Feb 09 '17 at 12:04
1
def count_values_that_are_keys(d):
    return sum([x in d.keys() for x in d.values()])

Use list comprehension to build a list with True/False. Sum treats True as 1 and False as 0.

Christian König
  • 3,437
  • 16
  • 28
  • 1
    ... you have no idea how close I was to writing `def obligatory_list_comprehension_method()` :P Upvote though, it's definitely a cleaner approach. – roganjosh Feb 09 '17 at 12:00
  • Thanks! This should definitely be enough for most cases. Optimization of membership tests with sets could probably be done if really needed. – Christian König Feb 09 '17 at 12:06
  • 1
    Yeah, I have never actually looked into the tipping point in terms of size for using `set` for this kind of thing. It probably isn't as high as I imagine actually but certainly not worth it here. – roganjosh Feb 09 '17 at 12:08
0
def count_values_that_are_keys(d):
    '''(dict) -> int

    Return the number of values in d that are also keys in d.
   
    >>> count_values_that_are_keys({1: 2, 2: 3, 3: 3})
    3
    >>> count_values_that_are_keys({1: 1})
    1
    >>> count_values_that_are_keys({1: 2, 2: 3, 3: 0})
    2
    >>> count_values_that_are_keys({1: 2})
    0
    #x in d.keys()for x in d.values()
    '''
    
    result = 0
    for k in d:
        if d[k] in d.keys():
             result = result + 1

    return result
General Grievance
  • 4,555
  • 31
  • 31
  • 45