【问题标题】:Python Loop for temperature [closed]Python循环温度[关闭]
【发布时间】:2020-02-15 21:15:15
【问题描述】:

我是第一次学习 python,我的任务要求我:

编写一个循环来打印hourly_temperature 中的所有元素。分离 带有 -> 的元素被空格包围。给定的样本输出 程序:

90 -> 92 -> 94 -> 95 

注意:95 后跟一个空格,然后是换行符。

玩后遇到困难,发现编码答案为:

hourly_temperature = [90, 92, 94, 95]

Htemp = len(hourly_temperature)

#create list
temps = []

if Htemp >= 0:
    for i in hourly_temperature: #question on i and appends i
        temps.append(i)
        temps.append('->')
        Htemp -= 1
        continue
temps.pop()
for s in temps:    #question here
    print(s,'', end='')
    continue
print('')

我的问题是关于我用#question 标注的for 循环。

我想知道is 使代码工作的目的是什么。

【问题讨论】:

  • sopython.com/wiki/What_tutorial_should_I_read%3F;任何基础教程都会解释循环是如何工作的。
  • 这似乎不是您的代码,您正在尝试从中学习编程。我确信做任何事情来学习是一件好事,但你可以做得比这更好。这段代码很糟糕,有很多东西是不需要的,你可以找到更好的学习资源。另外,找一些解释代码作用的教程,这样你就不必猜测了。
  • 我从来没有说过这是我的代码,我说我遇到了困难并“找到”了编码的答案。我一直在努力的部分是添加 ->.
  • 我投票结束这个。 Stack Overflow 不能替代指南、教程或文档,在涉及学校作业时更是如此。

标签: python python-3.x list loops for-loop


【解决方案1】:

i 获取hourly_temperature 的每个值,就像s 获取temps 中每个元素的值一样。

这是用分隔符连接值的一种非常复杂的方法。最好使用str.join

hourly_temperature = [90, 92, 94, 95]
print(' -> '.join(map(str, hourly_temperature)))

【讨论】:

    【解决方案2】:

    我相信这里的很多人都会给你“Pythonic”的单行代码来输出你需要的东西。但我认为你正处于需要学习如何逻辑编码的阶段,而不是如何成为 Pythonic。

    您给出的示例在任何语言中都很杂乱且不优雅。

    以下是我将如何使用适合初学者的简单逻辑来编写您的作业。希望这将比简短的单行更有用。

    hourly_temperature = [90, 92, 94, 95]
    
    # Start with an empty string
    s = ""
    
    # Step through each temperature in the list
    for t in hourly_temperature:
    
        # If the string contains a number, then we need to add ->
        # before the next number.
        if s != "":
            s += "-> "
    
        # Add the temperature and a space
        s += str(t) + " "
    
    # Print the string we have just built
    print(s)
    

    实现结果的另一种常见方法是不用担心在循环期间检查“->”是否需要作为前缀。相反,始终添加前缀“->”并在末尾删除错误的那个。这对于大型循环来说效率更高。

    hourly_temperature = [90, 92, 94, 95]
    
    s = ""
    for t in hourly_temperature:
        s += " -> " + str(t)
    
    # At this stage, s contains " -> 90 -> 92 -> 94 -> 95" so we need to remove the first ->
    # We do this by keeping everything from character 4 onwards
    print(s[4:])
    

    【讨论】:

    • 这非常有用,非常有用,谢谢!我正在上一堂互联网课,只有 8 周,节奏如此之快,我很少看到事情解释得这么好。
    【解决方案3】:

    is 将成为每次循环迭代时表示列表中下一个值的变量。您需要它们来实际访问每个项目。

    您应该尝试重写您的答案以仅使用一个循环并避免创建第二个列表temps

    您也应该能够在不使用Htemppop()continue 的情况下执行此操作。

    【讨论】:

      【解决方案4】:

      你的例子很乱,可以做得很小
      例如:

      hourly_temperature = [90, 92, 94, 95]
      joined_temps = " -> ".join(map(str, hourly_temperature)) + " " # Add the temperatures together with " -> " as separaters and add the space as you said
      print(joined_temps)
      

      "separater".join(typing.Union[list, tuple]) 将一个列表或元组连接在一起,由给定的分隔符分隔,然后我们按照你说的添加空格

      然后我们打印它,因为 print 已经在字符串的末尾提供了一个换行符

      如果你想要一个 for 循环形式

      hourly_temperature = [90, 92, 94, 95]
      ordered_temps = []
      for temp in hourly_temperature:
          ordered_temps.append(temp)
          if not temp == hourly_temperature[-1]:
              ordered_temps.append(" -> ")
          else:
              ordered_temps.append(" ")
      print("".join(map(str, ordered_temps)))
      

      我们曾经再次使用 join 但这没有分隔符,所以它只会加入列表。

      【讨论】:

        猜你喜欢
        • 2015-07-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多