for flight in range(0,s_count-1):
for check in range(0,count-1):
if str(rmr[flight][0])==str(sec[check][0]) or str(rmr[flight][1])==str(sec[check][1]):
print 'Direct flight exist from '+str(sec[check][0])+' to '+str(sec[check][1])+', price'+str(sec[check][3])+', flight duration '+str(sec[check][2])+'\n'
else:
print 'There is no Direct flight from '+str(sec[check][0])+' to '+str(sec[check][1])+'\n'
break
S_COUNT = 6
计数= 8
count和s_count是表示我从中获取信息的两个特定文件中有多少行的数字。 该信息基本上如下
rmr = [[2,4],[2,3],[2,5],[4,5],[1,5]]
rmr[x][0]
代表'Origin',rmr[x][1]
代表'Destination'
sec =
[
[1,2,4.0,100.0],
[2,3,1.5,500.0],
[2,4,10.0,700.0],
[2,5,5.75,1500.0],
[3,4,11.4,200.0],
[4,5,10.5,750.0],
[4,6,6.75,550.0]
sec[x][0]
代表'Origin'
sec[x][1]
代表'目的地'
sec[x][2]
代表'时间'
sec[x][3]
代表“费用”
关于航班信息我想做的整个概念。根据{{1}}“路线匹配请求”,客户希望从'Origin'2转到'Destination'4
所以,如果我们转到rmr[0]
,我们会看到这个航班可用,需要10个单位的时间,费用为700,所以这就是外面应该说的:
直飞航班从2到4,价格700.0,飞行时间10.0
所以我运行了这个代码,这是输出
从1到2没有直飞航班
直飞航班从2到3,价格500.0,航班时长1.5
直飞航班从2到4,价格700.0,航班时长10.0
直达航班从2到5,价格1500.0,航班时长5.75
直飞航班从3到4,价格200.0,航班时长11.4
4至5没有直达航班
没有从4到6的直飞航班
虽然我的预期是
直飞航班从2到4,价格700.0,航班时长10.0
直飞航班从2到3,价格500.0,航班时长1.5
直达航班从2到5,价格1500.0,航班时长5.75
直飞航班从4到5,价格750.0,航班时长10.5
从1到5没有直飞航班
答案 0 :(得分:2)
您的循环索引没有问题,您遇到以下问题:
and
不是or
str
这将有效:
for flight in range(0,s_count-1):
found = False
for check in range(0,count-1):
if rmr[flight][0]==sec[check][0] and rmr[flight][1]==sec[check][1]:
found = True
print 'Direct flight exist from {0} to {1}, price {3}, duration {2}'.format(*sec[check])
break
if not found:
print 'There is no Direct flight from {} to {}'.format(*rmr[flight])
答案 1 :(得分:0)
问题很可能在于使用range
:
for flight in range(0,s_count-1):
for check in range(0,count-1):
如果你想拥有从0到s_count的数字,不包括s_count,你应该输入range(s_count)
。第二行也一样。
示例:
In [1]: list(range(5))
[0, 1, 2, 3, 4]
答案 2 :(得分:0)
试试这个:
s_count=5
count=7
rmr = [[2, 4], [2, 3], [2, 5], [4, 5], [1, 5]]
sec =[[1, 2, 4.0, 100.0],[2, 3, 1.5, 500.0],[2, 4, 10.0, 700.0],[2, 5, 5.75, 1500.0],[3, 4, 11.4, 200.0],[4, 5, 10.5, 750.0],[4, 6, 6.75, 550.0]]
for flight in range(0,s_count):
for check in range(0,count):
if str(rmr[flight][0])==str(sec[check][0]) and str(rmr[flight][1])==str(sec[check][1]):
print 'Direct flight exist from '+str(sec[check][0])+' to '+str(sec[check][1])+', price'+str(sec[check][3])+', flight duration '+str(sec[check][2])+'\n'
break
if check == count-1 and flight == s_count-1:
print 'There is no Direct flight from '+str(rmr[flight][0])+' to '+str(rmr[flight][1])+'\n'
这基本上检查以确保rmr和sec之间匹配,然后打印结果。如果你在循环的最后,你知道没有匹配,所以你可以指出。
这显然可以清理和优化一下,但它确实解决了我认为的直接问题。