擅长:python、mysql、java
<p><strong>1)</strong>您定义<code> same</code>和<code> diff</code>的方式要求后面有一个参数,该参数将分配给解析的参数名称空间。要使它们成为布尔标志,可以通过指定关键字参数<code>action='store_true'</code>来更改<a href="https://docs.python.org/dev/library/argparse.html#action" rel="nofollow">action</a>:</p>
<pre><code>parser.add_argument(' same',
help='Find those in List1 that are the same in List2',
action='store_true')
</code></pre>
<p><strong>2)</strong>您不将已解析的参数存储在变量中,而是尝试将它们作为局部变量引用,而不是在<code>parse_args()</code>返回的对象上:</p>
^{pr2}$
<p><strong>3)</strong>如果为参数指定<a href="https://docs.python.org/dev/library/argparse.html#type" rel="nofollow">^{<cd5>}</a>,则解析后的参数实际上已经是一个打开的文件对象,因此不要在其上使用<code>open()</code>:</p>
<pre><code>data1 = set(l.rstrip() for l in args.infile1)
</code></pre>
<hr/>
<p><strong>注意</strong>:当前用户可以合法地同时指定<code> same</code>和{<cd2>},因此您的程序需要处理这一点。您可能希望将这些标志<a href="https://docs.python.org/dev/library/argparse.html#mutual-exclusion" rel="nofollow">mutually exclusive</a>。在</p>