擅长:python、mysql、java
<p>如果您创建的函数定义了word的含义,那么可以使用<a href="https://docs.python.org/3/library/itertools.html#itertools.groupby" rel="nofollow noreferrer">^{<cd1>}</a>按此函数分组。然后您可以附加<code>join()</code>ed结果或<code>extend()</code>,这取决于它是否是一组字数。你知道吗</p>
<p>我从您的示例中推断,您将单词定义为不带数字的任何东西,但您可以根据自己的需要调整函数:</p>
<pre><code>from itertools import groupby
# it's a word if it has no numerics
def word(s):
return not any(c.isnumeric() for c in s)
def groupwords(s):
sub = []
for isword, v in groupby(s, key = word):
if isword:
sub.append(" ".join(v))
else:
sub.extend(v)
return sub
res =[groupwords(l) for l in nested_list]
res
</code></pre>
<p><strong>结果:</strong></p>
<pre><code>[['72010', 'PHARMACY -IV FLUIDS', '7.95'],
['TOTAL HOSPITAL CHARGES', '6,720.92'],
['PJ72010', 'WORD FLUIDS', '7.95']]
</code></pre>