尝试使用下划线和输入了解Python循环 [英] Trying to understand Python loop using underscore and input

查看:160
本文介绍了尝试使用下划线和输入了解Python循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另一个提示-如果有人在HackerRank上学习Python,知道这一点对于入门至关重要.

One more tip - if anyone is learning Python on HackerRank, knowing this is critical for starting out.

我正在尝试理解以下代码:

I'm trying to understand this code:

    stamps = set()
    for _ in range(int(input())):
        print('underscore is', _)
        stamps.add(raw_input().strip())
        print(stamps)

输出:

    >>>2 
    underscore is 0
    >>>first
    set(['first'])
    underscore is 1
    >>>second
    set(['second', 'first'])

  1. 我将2作为第一个原始输入.函数如何知道我只循环两次?这让我失望,因为这不是典型的...对于xrange(0,2)结构中的我而言.

  1. I put 2 as the first raw input. How does the function know that I'm only looping twice? This is throwing me off because it isn't the typical...for i in xrange(0,2) structure.

起初我的想法是下划线重复shell中的最后一个命令.因此,我在代码中添加了打印语句,以查看下划线的值...但这些值仅显示0和1,就像典型的循环结构一样.

At first my thinking was the underscore repeats the last command in shell. So I added print statements in the code to see the value of underscore...but the values just show the 0 and 1, like the typical loop structure.

我已经阅读了这篇文章,但仍然不明白使用了下划线的这3种用法中的哪一种.

I read through this post already and I still can't understand which of those 3 usages of underscore is being used.

单身人士的目的是什么下划线"_" Python中的变量?

我才刚刚开始学习Python,所以很容易进行解释!

I'm just starting to learn Python so easy explanations would be much appreciated!

推荐答案

ncoghlan的答案列出了在Python中:

ncoghlan's answer lists 3 conventional uses for _ in Python:

  1. 以交互方式保存最后执行的语句的结果 口译会议.这个先例是由标准CPython设定的 口译员,其他口译员也纷纷效仿
  2. 用于在i18n中进行翻译查找(从相应的C导入 ,我相信),例如以下代码:

  1. To hold the result of the last executed statement in an interactive interpreter session. This precedent was set by the standard CPython interpreter, and other interpreters have followed suit
  2. For translation lookup in i18n (imported from the corresponding C conventions, I believe), as in code like:

raise forms.ValidationError(_("Please enter a correct username"))`

  • 作为通用的"throwaway"变量名称来表示该部分 函数结果的故意被忽略,如代码所示:

  • As a general purpose "throwaway" variable name to indicate that part of a function result is being deliberately ignored, as in code like:

     label, has_label, _ = text.partition(':')
    


  • 您的问题是代码示例中使用的是哪一个.答案将是一个一次性变量(情况3),但其内容在此处打印以用于调试.


    Your question is which one of these is being used in the example in your code. The answer would be that is a throwaway variable (case 3), but its contents are printed here for debugging purposes.

    但是,如果以任何方式使用_作为循环变量,则不是通用的Python约定.因此,您经常会看到:

    It is however not a general Python convention to use _ as a loop variable if its value is used in any way. Thus you regularly might see:

     for _ in range(10):
         print("Hello world")
    

    其中,_立即向读者发出信号,该值并不重要,并且该循环仅重复了10次.

    where _ immediately signals the reader that the value is not important and it the loop is just repeated 10 times.

    但是在诸如

     for i in range(10):
         do_something(i)
    

    使用循环变量的值的地方,习惯上使用变量名称,例如ij而不是_.

    where the value of the loop variable is used, it is the convention to use a variable name such as i, j instead of _.

    这篇关于尝试使用下划线和输入了解Python循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆