Comparison Of Value Items In A Dictionary And Counting Matches
Solution 1:
I would recommend you take a look at for
loops, and how they work. Codeacademy is a great (free) interactive resource. I'm not 100% sure I understand what you're asking, so I'm mainly going off of your question title. I think I understand what you're trying to do, but if I do, you're making it really difficult on yourself.
Iterating a dictionary with a single item:
dicty = {'a': 1}
for key in dicty:
print key
Iterating through values and keys in dictionaries:
for key, val in dicty.iteritems():
print key
print val
which is equivalent to:
for key in stuff:
val = stuff[key]
print key
print val
Counting (key) matches in two dicts:
count = 0
keys_counted = []
dicty_a = {'key1': 'val1', 'key2': 'val2'}
dicty_b = {'key1': 'val1', 'keyB': 'valB'}
for keyA, valA in dicty_a.iteritems():
for keyB, valB in dicty_b.iteritems():
if keyB == keyA and keyA not in keys_counted:
count += 1
keys_counted.append(valA)
The most common solution to making sure you don't duplicate a check is to build a list, check against it, then throw it out at the end. For example:
list_printed = []
for x in range(5):
for y in range(5):
if [x, y] not in list_printed:
print x, y
list_printed.append([y,x])
outputs:
0 0
0 1
0 2
0 3
0 4
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Enumerate()
allows you to get both the keys and values of items in your dictionary, or the iteration number and value if you are looping through a list.
With regard to your issue with iterating over single items. Tuples are weird, especially with single items. You need the trailing comma or it's not a tuple, it's an int. Honestly, that strikes me as a bug, not a feature, but optimistically, it's probably important for some computer-sciency reason that's way beyond my paygrade. Also, the big thing that distinguishes lists []
and tuples ()
is that tuples are immutable. You probably don't need to be using tuples in this case. Having said that, you were getting your error just because, you said, you can't iterate an integer:
loopy = (1,)
for x in loopy:
print x # this will _not_ raise an error
loopy = (1)
for x in loopy:
print x # this _will_ raise an error
loopy = [1]
for x in loopy:
print x # this will _not_ raise an error
loopy = 1
for x in loopy:
print x # this _will_ raise an error
Applying this to what you're trying to do:
dicModul = {0:(0,1),1:(1,1), 2:(2,3,4,5,6,1,7,2),
3:(8,1),4:(9,9), 5:(10,10,5,11,0,12,13), 6:(10,11,9,7)}
keys_checked = []
def get_tuple_matches(t1, t2):
counter = 0
matched_list = []
for x in t1:
for y in t2:
stuff = [x, y]
if stuff not in matched_list and x == y:
counter +=1
matched_list.append(stuff)
return counter
for key_outerloop, val_outerloop in dicModul.iteritems():
for key_innerloop, val_innerloop in dicModul.iteritems():
if key_outerloop == key_innerloop:
print "skip..."
elif [key_innerloop, key_outerloop] not in keys_checked:
matches = get_tuple_matches(val_outerloop, val_innerloop)
keys_checked.append([key_outerloop, key_innerloop])
print "Modul: " + str(key_outerloop) + " | Modul: " + str(key_innerloop) + " | Matches= "+ str(matches)
output:
skip...
Modul: 0 | Modul: 1 | Matches= 1
Modul: 0 | Modul: 2 | Matches= 1
Modul: 0 | Modul: 3 | Matches= 1
Modul: 0 | Modul: 4 | Matches= 0
Modul: 0 | Modul: 5 | Matches= 1
Modul: 0 | Modul: 6 | Matches= 0
skip...
Modul: 1 | Modul: 2 | Matches= 1
Modul: 1 | Modul: 3 | Matches= 1
Modul: 1 | Modul: 4 | Matches= 0
Modul: 1 | Modul: 5 | Matches= 0
Modul: 1 | Modul: 6 | Matches= 0
skip...
Modul: 2 | Modul: 3 | Matches= 1
Modul: 2 | Modul: 4 | Matches= 0
Modul: 2 | Modul: 5 | Matches= 1
Modul: 2 | Modul: 6 | Matches= 1
skip...
Modul: 3 | Modul: 4 | Matches= 0
Modul: 3 | Modul: 5 | Matches= 0
Modul: 3 | Modul: 6 | Matches= 0
skip...
Modul: 4 | Modul: 5 | Matches= 0
Modul: 4 | Modul: 6 | Matches= 1
skip...
Modul: 5 | Modul: 6 | Matches= 2
skip...
Post a Comment for "Comparison Of Value Items In A Dictionary And Counting Matches"