There is a dictionary. Keys are aids, ip values. How to find out which keys have the same values? The dictionary has the form

'10214': '192.168.15.11', '10215': '192.168.15.12', '10216': '192.168.15.9', '10217': '192.168.15.7', '10218': '192.168.15.10', '10219': '192.168.15.102', '10220': '192.168.15.103', '10221': '192.168.15.104', '10222': '192.168.15.8', '10223': '192.168.15.36', 

I want to know what aipishki duplicate. Tell me how this can be done.

    2 answers 2

    To find out which ip occur more than once:

     from collections import Counter dups = {ip for ip, count in Counter(id2ip.values()).items() if count > 1} # -> {'192.168.15.102', '192.168.15.8'} 

    See How to find all duplicate items in the list and the number of repetitions?

    To find out which id correspond to repeating ip, to group id by ip:

     from collections import defaultdict ip2ids = defaultdict(list) for id, ip in id2ip.items(): ip2ids[ip].append(id) for ip, ids in ip2ids.items(): if len(ids) > 1: print(f'{ip:15s}:', *ids) 

    Result:

     192.168.15.8 : 10216 10222 192.168.15.102 : 10218 10219 
       my_dict = {'10214': '192.168.15.11', '10216': '192.168.15.8', '10218': '192.168.15.102', '10219': '192.168.15.102', '10220': '192.168.15.103', '10221': '192.168.15.104', '10222': '192.168.15.8', '10223': '192.168.15.36',} for x,y in my_dict.items(): if list(my_dict.values()).count(y) > 1: print(x,y) 

      Conclusion:

      10216 192.168.15.8
      10218 192.168.15.102
      10219 192.168.15.102
      10222 192.168.15.8

      • swears on .count, says that there is none. - kolas
      • sorry corrected for third python - JaponDemon
      • can, then, still prompt how to display all keys on the same value? - kolas
      • kolas, print (x, y) x is the key, y is the value. simply means print (x) - JaponDemon
      • 2
        This is a quadratic algorithm, there is a simple linear solution. See at the end of the answer about the difference - jfs