擅长:python、mysql、java
<p>首先-这与CSV本身没有什么关系。如果你想像这里一样计算值,使用字典是个好主意,所以你需要的是这样的东西(我假设动物和食物是列表):</p>
<pre><code>counts = {}
for animal, food in zip(animals, foods):
counts.setdefault((animal, food), 0)
counts[(animal, food)] += 1
</code></pre>
<p>在这个循环之后,您将拥有一个字典,其中的键是(动物、食物)元组,值是计数。因此您可以将它们写入csv,如:</p>
<pre><code>for ((animal, food), count) in counts.items():
csv_writer.writerow([animal, food, count])
</code></pre>