回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>还有一个技巧——如果有人在HackerRank上学习Python,那么知道这一点对于开始学习是至关重要的。</p>
<p>我试图理解这段代码:</p>
<pre><code> stamps = set()
for _ in range(int(input())):
print('underscore is', _)
stamps.add(raw_input().strip())
print(stamps)
</code></pre>
<p>输出:</p>
<pre><code> >>>2
underscore is 0
>>>first
set(['first'])
underscore is 1
>>>second
set(['second', 'first'])
</code></pre>
<ol>
<li><p>我把2作为第一个原始输入。函数如何知道我只循环两次?这让我大吃一惊,因为它不是典型的…对于我来说,在xrange(0,2)结构中。</p></li>
<li><p>起初我的想法是下划线重复shell中的最后一个命令。所以我在代码中添加了print语句来查看下划线的值……但是这些值只显示0和1,就像典型的循环结构一样。</p></li>
</ol>
<p>我已经读完了这篇文章,我仍然不明白这三种下划线用法中的哪一种正在被使用。</p>
<p><a href="https://stackoverflow.com/questions/5893163/what-is-the-purpose-of-the-single-underscore-variable-in-python">What is the purpose of the single underscore "_" variable in Python?</a></p>
<p>我刚开始学习Python,所以非常感谢简单的解释!</p>