我能夠接近,但我認爲你需要更多的路線才能獲得完整的路線。它不能添加的一些價值沒有足夠的相似的周邊元素,但這可能會讓你沿着正確的道路前進。
bus_1 = ["5171","1337","1341","1350","1352","1357","320","278","10","15","215","218","1623","1624","7347"]
bus_2 = ["5171","2976","2979","2981","2991","2992","1326","1327","1329","1331","1336","1337","1339","1340","1342","1345","1350","1354","1357","1359","320","278","12","15","17","21","205","215","216","218","1624","1626","1627","7347"]
bus_3 = ["5171","2977", "2978","2991","1325","1326","1327","1329","1330","1331","1332","1333","1337","1340","1341","1342","1344","1345","1347","1348","1352","1353","1354","1355","1357","1359","320","278","10","12","14","15","17","19","21","85","204","215","218","219","220","1622","1623","1624","1625","1626","1627","7347"]
buses=[bus_1,bus_2,bus_3]
added=set()
final=bus_1[:]
temp=bus_1+bus_2+bus_3
i=0
x=0
while set(final) != set(temp):
# get 2 elements which we'll see if they're in final route
if x<len(buses):
t=buses[x][i:i+2]
else:
t=final[i:i+2]
try:
a=final.index(t[0])
b=final.index(t[1])
except:
a=b=-1
# check if in final and not next to each other, and not added yet
for q,bus in enumerate(buses):
if x!=q and t[0] in bus and t[1] in bus and a+1==b and (t[0],t[1]) not in added:
u=bus.index(t[0])
v=bus.index(t[1])
final[a:b]=bus[u:v]
added.add((t[0],t[1]))
i=0
x=0
if x<len(buses):
for q,bus in enumerate(buses):
if q==x and i+2<len(bus):
i+=1
elif q==x and i+2==len(bus):
x+=1
i=0
elif x==len(buses)+1 and i+2<len(final):
i+=1
else:
x+=1
i=0
if x>len(buses)+1:
break
print("final route(incomplete)")
print(final)
print('unique stops')
print(set(temp))
#this is my result
#mine=['5171', '2976', '2979', '2981', '2991', '2992', '1326', '1327', '1329', '1330', '1331', '1336', '1337', '1339', '1340', '1341', '1350', '1352', '1353', '1354', '1355', '1357', '1359', '320', '278', '10', '12', '14', '15', '17', '19', '21', '205', '215', '216', '218', '219', '220', '1622', '1623', '1624', '1625', '1626', '1627', '7347']
#this is what you probably want
#goal=['5171', '2976', '2977', '2978', '2979', '2981', '2991', '2992', '1325', '1326', '1327', '1329', '1330', '1331', '1332', '1333', '1336', '1337', '1339', '1340', '1341', '1342', '1344', '1345', '1347', '1348', '1350', '1352', '1353', '1354', '1355', '1357', '1359', '320', '278', '10', '12', '14', '15', '17', '19', '21', '85','204', 205', '215', '216', '218', '219', '220', '1622', '1623', '1624', '1625', '1626', '1627', '7347']
#unable to add
#['204', '85', '1348', '1347', '1342', '1344', '1345', '1333', '1332', '1325', '2978']
請發佈預期結果。 –
複製品呢? –
究竟是什麼訂單? –