main_dict=
{1: {'Origin': '001', 'Destination': '002', 'Cost': '0100.00','Time': '04.00'},
2: {'Origin': '002', 'Destination': '003', 'Cost': '0500.00', 'Time': '01.50'},
3: {'Origin': '003', 'Destination': '004', 'Cost': '0200.00', 'Time': '11.40'},
4: {'Origin': '002', 'Destination': '004', 'Cost': '0700.00', 'Time': '10.00'},
5: {'Origin': '004', 'Destination': '006', 'Cost': '0550.00', 'Time': '06.75'},
6: {'Origin': '004', 'Destination': '005', 'Cost': '0750.00', 'Time': '10.50'},
7: {'Origin': '005', 'Destination': '006', 'Cost': '0460.00', 'Time': '08.00'},
8: {'Origin': '002', 'Destination': '005', 'Cost': '1500.00', 'Time': '05.75'}}
count=9
first_list=[]
second_list=[]
for c in range(1,count):
first_list.append(main_dict[c]['Origin']) #puts all origins in one list
second_list.append(main_dict[c]['Destination'])#puts all destinations iin one list
locations=[]
locations.extend(first_list)
locations.extend(second_list)
locations=(list(set(locations)))#gets rid of any duplicates
locations.sort()
mat_nxn = [[None for x in range(len(locations))] for y in range(len(locations))] #in this section the main matrix is created
for i in range(len(locations)):
mat_nxn[0][i]=locations[i] #fills the first row with the locations
mat_nxn[i][0]=locations[i] #fills the first column with the locations
for n in range(0,len(locations)-1):
for i in range(0,len(locations)):
if str(mat_nxn[0][n])==main_dict[n+1]['Origin'] or str(mat_nxn[i][0])==main_dict[i+1]['Destination'] :
a=int(mat_nxn[0][n])
b=int(mat_nxn[n][0])
mat_nxn[b][a]=main_dict[n+1].values()
So what my code is supposed to do is arrange the dictionary's info in a NxN matrix, how it work is that the "Origin" and "Destination" are the "borders" of the martix enter image description here Then if let us say I can go from "Origin" to a "Destination" as stated in the dictionary I will add it to the matrix under an example would be, in the first dictionary I can go from "Origin 001" to "Destination 002" so I will place the values of the dictionary under X,Y(001,002) in the matrix My problem is in the last part of the code where I used the if
condition with or
inside two for
loops
for n in range(0,len(locations)-1):
for i in range(0,len(locations)):
if str(mat_nxn[0][n])==main_dict[n+1]['Origin'] or str(mat_nxn[i][0])==main_dict[i+1]['Destination'] :
Now the problem is that if I have a duplicate "Origin" in my case it is 002 it will not check the rest "Destinations", only the first one, how can I make it check them all, did I use the or
the wrong way? Would appreciate any help
Aucun commentaire:
Enregistrer un commentaire