comparison of the values of two dictionnaries
AnsweredHi,
I want to compare the values of two given dict and print an output if they are identic. But it gives me only "zero" for all cases, although some elements are identic.
Something goes wrong by the iteration, but i can't fix it.
Thks for support
Geoffrey
Aisle_arct={1:['m','b','p','d','g'], 2:['y','i','a','e','h'], 3:['n','q','c','f','j']}
Order={1:['h', 'p', 'g', 'j', 'm', 'i', 'c', 'a', 'b', 'n', 'f', 'q', 'd'], 2:['f', 'y', 'e', 'q', 'a', 'c'], 3: ['c'], 4: ['a', 'h', 'c', 'i']}
dvar={}
for i in Order:
for z in Order[i]:
for k in Aisle_arct:
for j in Aisle_arct[k]:
if (z==j):
dvar[i,k]=L
else:
dvar[i,k]=0
print(dvar)
Output
{(1, 1): 0, (1, 2): 0, (1, 3): 0, (2, 1): 0, (2, 2): 0, (2, 3): 0, (3, 1): 0, (3, 2): 0, (3, 3): 0, (4, 1): 0, (4, 2): 0, (4, 3): 0}
-
Please note that this is not a Gurobi question but a general Python question and should be and should be rather posted in a Python forum instead of here.
Anyway, you are overwriting your \(\texttt{dvar}\) values with \(0\). For example, when you check for \(\texttt{Order[1][4]}\) and \(\texttt{Aisle_arct[1][0]}\), you will temporarily have a value of \(\texttt{dvar[1,1]=L}\). However, the last comparison for \(\texttt{i=1, k=1}\) is \(\texttt{Order[1][12]='d'}\) \(\neq\) \(\texttt{'g'=Aisle_arct[1][4]}\) and thus you get \(\texttt{dvar[1,1]=0}\).
I don't know what you want to do with the \(\texttt{dvar}\) dict, but one way (probably not the most pythonic one) to get only the matching characters would be
dvar={}
for i in Order:
for k in Aisle_arct:
dvar[i,k] = []
for z in Order[i]:
for j in Aisle_arct[k]:
if (z==j):
dvar[i,k].append(z)
print(dvar)Best regards,
Jaromił0
Please sign in to leave a comment.
Comments
1 comment