擅长:python、mysql、java
<p>改变</p>
<pre><code>writer.writerow(row[2])
</code></pre>
<p>到</p>
^{pr2}$
<p>另外,检查列表中的成员关系在计算上是非常昂贵的[O(n)]。如果您要检查大量项集合中的成员资格,并且经常这样做,请使用<code>set</code>[O(1)]:</p>
<pre><code>L = set()
reader.next() # Skip the header
for row in reader:
if row[2] not in L:
L.add(row[2])
writer.writerow([row[2]])
</code></pre>
<h3>或者</h3>
<p>如果您可以使用几兆字节的内存,请执行以下操作:</p>
<pre><code>with open("sample.csv", "rb") as infile:
reader = csv.reader(infile)
reader.next()
no_duplicates = set(tuple(row) for row in reader)
with open("users.csv", "wb") as outfile:
csv.writer(outfile).writerows(no_duplicates)
</code></pre>
<p>如果顺序很重要,请使用<code>OrderedDict</code>而不是集合:</p>
<pre><code>from collections import OrderedDict
with open("sample.csv", "rb") as infile:
reader = csv.reader(infile)
reader.next()
no_duplicates = OrderedDict.fromkeys(tuple(row) for row in reader)
with open("users.csv", "wb") as outfile:
csv.writer(outfile).writerows(no_duplicates.keys())
</code></pre>