0

I have a dictionary that looks like this:

entryDir = {'cities': [['US', 'AU']], 'countries': [['DK', 'US']]}

And I'm trying to figure out how to print a confirmation if the values for different keys match within each list. in pseudo code:

for each key:
    # {cities} {countries}
    if a value for that key matches the value of another key:
        #cities:US == countries:US
        print key1:value1,key2:value2,"match found" 
        #cities:US,countries:US,"match found
    if they don't match:
        #cities:AU == countries:DK
        print key1:value2,key2:value1,"no match"
        cities:AU,countries:DK,"no match found"

What I have so far is:

for key1 in entryDir:
    for key2 in entryDir:
        if key1 != key2:                        
            if entryDir[key1][0] == entryDir[key2][0]:
                print entryDir[key1][0],entryDir[key2][0],"match found"
            if entryDir[key1][0] != entryDir[key2][0]:
                print entryDir[key2][0],entryDir[key2][0],"no match found"

But this isn't correctly matching or not matching.

adam
  • 940
  • 5
  • 13
  • 30

2 Answers2

-1

You are also comparing a key's value to it's own values. Try instead:

for key1 in entryDir:
    for key2 in entryDir:
        if key1 != key2:
            if entryDir[key1][0] == entryDir[key2][0]:
                print entryDir[key1][0],entryDir[key2][0],"match found"
            else:
                print entryDir[key2][0],entryDir[key2][0],"no match found"

EDIT:

If you expect to have multiple values per key you need to do it like this:

for key1 in entryDir:
    for key2 in entryDir:
        if key1 != key2:
            for val in entryDir[key1]:
                if val in entryDir[key2]
                    print val, "match found"

Be aware of the (possibly) horrible time this needs to compute depending on the size of your input.

Robsdedude
  • 1,292
  • 16
  • 25
  • good catch, but it still doesn't account for having multiple values per key, which I'm unsure of how to handle. – adam Aug 10 '15 at 20:05
  • that's it! re: speed isues - is there a better way of approaching this problem that would be faster/more efficient? – adam Aug 10 '15 at 20:20
  • At least not theoretically speaking (It's O(n²*m) where n is the size of entryDir and m is the average length of the values). It may get faster if you use other build in functions or some c libraries. – Robsdedude Aug 10 '15 at 20:26
  • Well I thought about it again. You could speed it up by using some sort of a search tree but that would be quite complex. Or if you don't want to know the keys of the found match but only if there is a match, then you could flatten the data structure to be just a list and then do this: http://stackoverflow.com/questions/1541797/in-python-how-to-check-if-there-are-any-duplicates-in-list – Robsdedude Aug 11 '15 at 07:01
  • I figured out a better way of going about it, see below – adam Aug 11 '15 at 14:33
-1

I've solved it. I can use the indexing for the array generated by entryDir.keys() as an index for comparing values in entryDir.

So, instead of:

for key1 in entryDir:
    for key2 in entryDir:
        if key1 != key2:
            for val in entryDir[key1]:
                if val in entryDir[key2]
                    print val, "match found"

we can do:

keyList=entryDir.keys()
if keyList[0] != keyList[1]:                                            
    if entryDir[keyList[0]][0] == entryDir[keyList[1]][0]:
        print entryDir[keyList[0]][0], "match found"
    else:
        print entryDir[keyList[0]][0],entryDir[keyList[1]][0], "no match"
adam
  • 940
  • 5
  • 13
  • 30
  • So you assume that your input is a dict with exactly two elements and only the first value of each element's list matters? You should have included that information into you original question... – Robsdedude Aug 12 '15 at 08:15
  • not always, and logic could easily be added to iterate over and alias the keys in `keylist`. I just didn't for this example. – adam Aug 12 '15 at 13:20