擅长:python、mysql、java
<p>pythonic解决方案是使用生成器表达式,即可由函数使用的for循环:</p>
<pre class="lang-py prettyprint-override"><code>with jsonlines.open('myfile.jsonl') as readfile:
total_tokens = sum(len(contents['tokens']) for contents in readfile)
print(total_tokens)
</code></pre>