擅长:python、mysql、java
<p><strong>替换元组中的空白</strong></p>
<p>您不是要删除字符串的一部分,而是要从元组中删除空字符串(在<code>foundFiles</code>中有一个<a href="http://www.diveintopython.net/native_data_types/lists.html" rel="nofollow">list</a>或{a2}),可以这样做:</p>
<p><em>注意:由于元组是不可变的,一旦定义了元组,我们就无法对其进行编辑</p>
<pre><code>foundFilesFixed = [str(value for value in file if value) for file in foundFiles]
</code></pre>
<p>这将把<code>foundFiles</code>中的所有元组值复制到<code>foundFilesFixed</code>中,只要它们不是false(空白、null等)。在</p>
<p>这将取代:</p>
^{pr2}$
<p>有了这个:</p>
<pre><code>[
'bg.APDS.UnitTests.vbproj'
'bg.DatabaseAPI.UnitTests.vbproj'
'bg.DataManagement.UnitTests.vbproj'
'bg.FormControls.UnitTests.vbproj'
]
</code></pre>
<p>我假设所有的元组都有两个值,一个是空的,一个是文件名。如果这些值可能包含多个值,则需要将函数中的<code>str(</code>更改为<code>tuple(</code>。在</p>
<p><strong>备选方案:特定于应用程序的</strong></p>
<p>正如约旦在评论中指出的,你可以这样做:</p>
<pre><code>return [str(value[1]) for value in foundFiles]
</code></pre>
<p>然而,这并不是<code>return foundFiles</code>,它不太可能适用于未来的读者,所以不想引起人们的关注。在</p>