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

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

问题描述

另外一个提示 - 如果有人在 HackerRank 上学习 Python,那么了解这一点对于开始至关重要.

我正在尝试理解这段代码:

 邮票 = set()对于 _ 范围内(int(输入())):print('下划线是', _)邮票.添加(原始输入().带())打印(邮票)

输出:

 >>>2下划线为 0>>>首先设置(['第一个'])下划线是 1>>>第二设置(['第二','第一'])

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

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

我已经通读了这篇文章,但我仍然无法理解使用了下划线的 3 种用法中的哪一种.

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

我刚刚开始学习 Python,所以非常感谢简单的解释!

解决方案

ncoghlan 的回答 列出了 _ 在 Python 中:

<块引用>

  1. 在交互式中保存最后执行的语句的结果口译会议.这个先例是由标准 CPython 设定的口译员,其他口译员也纷纷效仿

  2. 用于 i18n 中的翻译查找(从对应的 C约定,我相信),就像在代码中一样:

    raise forms.ValidationError(_(请输入正确的用户名"))

  3. 作为通用的一次性"变量名称以指示该部分一个函数结果被故意忽略,如在代码中:

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


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

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

 for _ in range(10):打印(你好世界")

其中 _ 立即向读者表明该值并不重要并且循环仅重复 10 次.

但是在诸如

之类的代码中

 for i in range(10):做某事(一)

在使用循环变量的值的地方,约定使用ij等变量名代替_.

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)

Output:

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

  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.

  2. 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.

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

What is the purpose of the single underscore "_" variable in Python?

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

解决方案

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

  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"))
    

  3. 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(':')
    


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.

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")

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

However in a code such as

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

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

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

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