1

I'm trying to compare two dictionary indexes and return a list of indexes that match regardless of order. I am also trying to do this without loading any packages/libraries.

(If this was SQL, it would be an INNER JOIN of two tables.)

I tried doing this: Finding matching keys in two large dictionaries and doing it fast

But it this will only return the exact position where the dictionary indexes match.

I also tried this: https://learning.oreilly.com/library/view/python-cookbook/0596001673/ch01s09.html

"Problem: Given two dictionaries, you need to find the set of keys that are in both dictionaries." (This is exactly what I'm trying to do).

#Attempt 1 (using 1st source from Stack Overflow):

dict1 = {'x': [8, 8, 1, 8, 2, 1], 'v': [0.98, 0.24, 0.91, 0.03, 0.04, 0.75]}
dict2 = {'x': [0, 8, 8, 1, 3, 3], 'v': [0.98, 0.78, 0.66, 0.08, 0.42, 0.21, 0.04]}

def common_indx(dict1,dict2):
    dict1Set = set(dict1['x'])
    dict2Set = set(dict2['x'])

    for name in dict1Set.intersection(dict2Set):
        return(name)

common_indx(dict1,dict2)

Out: 8
#Attempt 2 (Using Python Cookbook):

intersect = []
for item in some_dict.keys(  ):
    if another_dict.has_key(item):
        intersect.append(item)
print "Intersects:", intersect

This doesn't work because it's in python 2 but the book is answering exactly what I need to do.

My expected outcome is [1,8], which are only the indexes that match in both dictionaries, regardless of position.

Jacky
  • 710
  • 2
  • 8
  • 27
  • 1
    You are intersection *two lists*, not two dictionaries. `dict1['x']` and `dict2['x']` are expressions to produce those lists. – Martijn Pieters Sep 01 '19 at 17:48

1 Answers1

2

Your error is that you return immediately for the first name:

for name in dict1Set.intersection(dict2Set):
    return(name)

You'd want to turn the intersection into a list instead:

return list(dict1Set.intersection(dict2Set))

or just return the set itself, without conversion to a list. You can also use the & operator on two sets:

return list(set(dict1['x']) & set(dict2['x']))

The Python cookbook code intersects the keys, while your own code intersects the values of a single key. That's something different.

It otherwise doesn't work because the dictionary.has_key() method is removed in Python 3, but you can use key in dictionary instead. You can also intersect the dictionary view objects for the keys:

shared_keys = dict1.keys() & dict2.keys()  # The intersection of the keys
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • This worked! Thank you Martijn! However, shared_keys did not work as intended. I have noticed that whenever i call the .keys() method I just get the names of the index and values. So for this example, I would get {'v', 'x'} Do you know what is causing this to happen? – Jacky Sep 01 '19 at 17:55
  • @Jacky: yes, because those are your dictionary keys. Note that there is nothing in Python dictionaries we call 'indexes', so I wrongly assumed that that's what you meant by the term. And the Python codebook code you found *only* looks at the keys. – Martijn Pieters Sep 01 '19 at 17:57
  • @Jacky: I addressed that code only because you had tried it, so I explained why it breaks on Python 3, how to fix that, and how to do that same task properly. But it isn't what you were looking for to begin with. – Martijn Pieters Sep 01 '19 at 17:58
  • That makes a lot of sense. The concept of a dictionary has been quite confusing to me but your explanation has cleared things up very well. – Jacky Sep 01 '19 at 18:10