0
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()
那麼,什麼我的代碼是應該做的是安排在一個N×N的矩陣,它是如何工作的字典的信息是,「原產地」和「目的地」是「邊界」在martix的重複第二如果條件使用或
那麼,如果讓我們說我可以從「起源」去在同一字典陳述「目的地」,將它添加到下右側的矩陣(X ,Y)的矩陣 一個例子是,在第一個字典中我可以從「Origin 001」到「Destination 002」,所以我會把字典的值放在u的nDer X,Y(001,002)的矩陣 我的問題是在我使用內部的兩個for
的if
條件與or
代碼的最後一部分循環
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'] :
現在的問題是,如果我有一個重複「Origin」在我的情況下是002它不會檢查剩餘的「Destinations」,只有第一個,所以我的輸出不能完成。我怎樣才能讓它檢查所有這些?我是否以錯誤的方式使用or
? 希望得到任何幫助
輸出必須是一個絕對矩陣(列表清單)?或者它可以是另一個模仿它的結構,就像是一個字典詞典? –
不,字典的字典可以很好地適用,但請記住,如果沒有從「起源」到「目的地」的旅行,(X,Y)應該顯示無 – Enigma