如何显示在这种情况下为 1 的第一站?

Posted

技术标签:

【中文标题】如何显示在这种情况下为 1 的第一站?【英文标题】:How can I show first station which in this case is 1? 【发布时间】:2021-08-14 07:22:04 【问题描述】:

现在火车在第一站,每个站都会停,除非到达第三站,不会停在那里。当我们到达最后一站时,我们会说再见。

我已经解决了这个问题,只是我未能在与 print 相同的语句中显示第一站。

current_station = 1
skip_station = 3
end_station = 7

# true if train isn't at the end station
while current_station < end_station:
    current_station = current_station + 1
    # if train reached  station # 3, skip that station
    if current_station == skip_station:
        continue
    else:
        print(current_station)
print('bye')

【问题讨论】:

简单的解决方案:在循环之前再打印一个。 提示:您可以使用current_station += 1,因为它与current_station = current_station + 1的含义相同 【参考方案1】:
current_station = 1
skip_station = 3
end_station = 7
# Because now the train will be leaving
if current_station==1:
    print(f'Train left from station: current_station')
# true if train isn't at the end station
while current_station < end_station:
    current_station = current_station + 1
    # if train reached  station # 3, skip that station
    if current_station == skip_station:
        continue
    else:
        print(f"The Train is at station: current_station")

【讨论】:

【参考方案2】:
current_station = 1
skip_station = 3
end_station = 7

# true if train isn't at the end station
while current_station < end_station:
    leaving_station = current_station
    current_station = current_station + 1
    # if train reached  station # 3, skip that station
    if current_station == skip_station:        
        continue
    else:
        print(leaving_station)
print('bye')

更新:假设我们只考虑离线算法:

current_station = 1
skip_station = 3
end_station = 7

for station in range(current_station, end_station):
    if station != skip_station:
        print(station)


print("Bye")

或者一些骇人听闻的单行:

from collections import deque

current_station = 1
skip_station = 3
end_station = 7


deque(map(print, (i for i in range(current_station, end_station) if i != skip_station)), maxlen=0)
print("bye")

对于所有示例,我假设end_station没有被火车访问。否则只需加 1 即可。

【讨论】:

唯一的编辑不是next_station,应该是leave_station 好吧,cmets 中的代码格式无法按预期工作,但我很难解析您的评论。我只是尝试对您的代码进行最少的修改,以使其执行您想要的操作。我会用另一个版本更新我的答案.. 哎呀.. 已修复 :-) @greybeard,谢谢!对不起,我昨天没有带笔记本电脑。只是想展示一些想法。现在我已经修复了,所有的例子都可以运行了!【参考方案3】:

同时使用

print(current_station)
while current_station < end_station:
    current_station += 1
    if current_station == skip_station:
        continue
    print(current_station)
print('bye')

使用for循环

for i in range(current_station,end_station):
    if i == skip_station:
        continue
    print(current_station)
print('bye')

【讨论】:

嗨@sittsering,感谢分享。您的代码有效,但我想在学习时应用 continue 语句。 @SandraRay 哦,如果是这样,您可以按照其他人的建议在循环之前放置一张打印件。 虽然您的原始代码和使用continue 的修订版都可以轻松解决问题:您能否评论重复print() 调用? for … in range(current_station, …)呢? @greybeard 你也可以用for循环,因为op用了while循环,我也用它来解决它。 @greybeard 我现在删除了else,不需要了。

以上是关于如何显示在这种情况下为 1 的第一站?的主要内容,如果未能解决你的问题,请参考以下文章

基于另一列向数据框添加列索引(在这种情况下为用户)

为啥尾随 %20 (在这种情况下为有效数据)会杀死 asp.net mvc 路由

如何在没有 MediaExtractor 的情况下为 H264 使用 MediaCodec

允许用户在我的类中定义数组大小而不是使用固定变量的最佳方法是啥,在这种情况下为 8

使用python在文本文件的每一行末尾添加一个特定的字符串(在这种情况下为“\\\hline”以准备一个乳胶表)

如何在不使用 Android Studio 的情况下为 Google Play 开发控制台创建即时应用程序?