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.