擅长:python、mysql、java
<p>您可以简单地累积长度:</p>
<pre><code>with jsonlines.open('myfile.jsonl') as readfile:
total = 0
for contents in readfile:
total += len(contents['tokens'])
print(total)
</code></pre>
<p>或者在适当的生成器表达式上使用<code>sum</code>:</p>
<pre><code>with jsonlines.open('myfile.jsonl') as readfile:
total = sum(len(contents['tokens']) for contents in readfile)
print(total)
</code></pre>