【问题标题】:can't control loop index无法控制循环索引
【发布时间】:2017-01-04 20:16:44
【问题描述】:
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

count=8

count 和 s_count 是代表我从中获取信息的两个特定文件中有多少行的数字。 信息基本如下

rmr = [[2, 4], [2, 3], [2, 5], [4, 5], [1, 5]]

rmr[x][0] 代表“起点”,rmr[x][1] 代表“终点”

秒 =

[

[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] 代表“原点” sec[x][1] 代表“目的地” sec[x][2] 代表“时间” sec[x][3] 代表“成本”

我试图对航班信息所做的整个概念。根据rmr[0] "Route Matching Request",客户希望从 'Origin' 2 转到 'Destination' 4

所以如果我们去sec[2],我们会看到这个航班是可用的,它需要10个单位的时间和700,所以这就是out应该说的:

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 的直飞航班

【问题讨论】:

  • 我建议的一件事是,如果这是家庭作业,请提及 -> 这是大学 CS 课程中非常常见的要求,您显然需要一些额外的帮助。我建议去看导师或你的教授。

标签: python loops indexing


【解决方案1】:

你的循环索引没有问题,你有问题:

  • 错误的条件:你想要and 而不是or
  • break 位置错误:当你找到一个直接的时候你想停下来
  • 错误使用 else
  • 没有理由在任何事情上都使用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])

【讨论】:

  • 我推荐一件事:print 'Direct flight exist from {0} to {1}, price {3}, duration {2}'.format(*sec[check]) & print 'There is no Direct flight from {} to {}'.format(*rmr[flight]) 通过解包很好地清理它。
  • 其实我知道str() 是不需要的,但我后来添加了它以检查它是否重要,非常感谢
【解决方案2】:

问题很可能出在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]

【讨论】:

  • 嗯,如果是这样的话,他的输出的开头会是一样的,而结尾会不同......?
  • 并没有真正改变任何事情
【解决方案3】:

试试这个:

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 之间存在匹配,然后打印结果。如果您处于循环的末尾,您就知道没有匹配项,因此您可以指出这一点。

这显然可以清理和优化一点,但我相信它确实解决了你的直接问题。

【讨论】:

    猜你喜欢
    • 2013-03-20
    • 2022-01-18
    • 1970-01-01
    • 2019-01-21
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多