The original Python 2 answer
This example uses izip
(instead of zip
) to avoid creating a new list and having to keep it in the memory. It also makes use of Python's built in csv module, which ensures proper escaping. As an added bonus it also avoids using any loops, so the code is short and concise.
import csv
from itertools import izip
with open('some.csv', 'wb') as f:
writer = csv.writer(f)
writer.writerows(izip(bins, frequencies))
The code adapted for Python 3
In Python 3, you don't need izip
anymore—the builtin zip
now does what izip
used to do. You also don't need to open the file in binary mode:
import csv
with open('some.csv', 'w') as f:
writer = csv.writer(f)
writer.writerows(zip(bins, frequencies))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…