Im using Python 2.7. Im trying to compare the value items in a dictionary.
I have two problems. First is the iteration of values in a dictionary with a length of 1. I always get an error, because python doesn't iterate integer and a single item as value is an integer for python. I have tried to change the item to a string. I have tried to change the whole dictionary to string. In these cases the iteration compares commas and brackets, as well and thats not the purpose. Therefore I duplicated the item. The values in key:1 and key:4 are one and the same item, two times. This solution works, but it changes a little bit the results, naturally.
The second problem is that the counting of matches doesn't work correctly. Yes, because of the first problem, but there is another one. My code compares all value items, with all other value items in the dictionary. Same moduls are not compared. If there is one match in a comparison, there is no problem. But if a comparison had two matches, the code will outputs the result two times as one match. But I want just one result, with two matches. The code includes many comments, so I hope you can unterstand it.
#dicModul is a dictionary with the modulnames(keys of dictionary) and the components of the modul(value of dictionary).
# If a modul consists of one component, it is impossible to iterate the modul, therefore this component is two times in a modul.
# Any ideas how to iterate a single value item in a dictionary?
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)}
#The list is needed for the iteration and to operate the different modul in the dictionary by following for loops.
ModulList = [0,1,2,3,4,5,6]
#Counter is needed for counting same components in different moduls.
Counter = 0
for modKey in ModulList: #For-loop is needed for iteration of keys
for modKey2 in ModulList: #2nd for-loop of Modulkeys.The components of moduls have to be checked for matches in other moduls.
#Therefore a second iteration of the modul list is needed.
if modKey == modKey2: #Same moduls do not have to be checked.
print "Skip same Moduls"
Counter = 0 #The modkey2 have to changed by iteration. The counter have to be reset.
elif modKey != modKey2: #Different moduls have to be checked.
for modVal in dicModul[modKey]: #Iteration of components for iterated modul.
if modVal in dicModul[modKey2]: #Checking iterated components in different modul
Counter = Counter + 1 #If component was in different moduls, counter will add +1
print "Modul: "+str(modKey)+" % Modul: "+str(modKey2)+ " Matches= "+str(Counter)
#print function for both moduls and number of same components
Counter =0
I think, if the can separte between the previous checked key and checked key at this juncture, the counter will not start at 0, every time. I tried to solve the problem and search for solutions, but I didn`t found this case.
loop on a dictionary (previous values) python
How to access previous/next element while for looping?
Count items in dictionary
I guess the solution have to look like the next code.
I mark my suggestion with #->
#dicModul is a dictionary with the modulnames(keys of dictionary) and the components of the modul(value of dictionary).
# If a modul consists of one component, it is impossible to iterate the modul, therefore this component is two times in a modul.
# Any ideas how to iterate a single value item in a dictionary?
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)}
#The list is needed for the iteration and to operate the different modul in the dictionary by following for loops.
ModulList = [0,1,2,3,4,5,6]
#Counter is needed for counting same components in different moduls.
Counter = 0
for modKey in ModulList: #For-loop is needed for iteration of keys
for modKey2 in ModulList: #2nd for-loop of Modulkeys.The components of moduls have to be checked for matches in other moduls.
#Therefore a second iteration of the modul list is needed.
if modKey == modKey2: #Same moduls do not have to be checked.
print "Skip same Moduls"
Counter = 0 #The modkey2 have to changed by iteration. The counter have to be reset
elif modKey != modKey2: #Different moduls have to be checked.
for modVal in dicModul[modKey]: #Iteration of components for iterated modul.
if modVal in dicModul[modKey2]: #Checking iterated components in different modul
#-> if modKey2 == previous modKey2: #Checking if is the previous modul, so counter is not reset.
#-> Counter = Counter +1
#-> print "Modul: "+str(modKey)+" % Modul: "+str(modKey2)+ " Matches= "+str(Counter)
#-> else:
#-> Counter = 1 #Counter is setted 1, because a same component is found in a different modul.
elif:
Counter = 0
This is my expected solution
Modul Modul Matches
skip same modul
0 1 1
0 2 1
0 3 1
0 4 0
0 5 1
skip same modul
1 2 1
1 3 1
1 4 0
1 5 0
skip same modul
2 3 1
2 4 0
2 5 1
2 6 1
skip same modul
3 4 0
3 5 0
skip same modul
4 5 0
4 6 1
skip same modul
5 6 3