I want to write data that I have to create a histogram into a csv file. I have my 'bins' list and I have my 'frequencies' list. Can someone give me some help to write them into a csv in their respective columns?

我想写一些数据,我必须在csv文件中创建一个直方图。我有我的'箱'列表,我有'频率'列表。有人可以给我一些帮助,将它们写入各自列中的csv吗?

ie bins in the first column and frequency in the second column

即第一列中的箱和第二列中的频率

3 个解决方案

#1


12

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.

此示例使用izip(而不是zip)来避免创建新列表并将其保留在内存中。它还使用了Python内置的csv模块,可以确保正确的转义。作为额外的奖励,它还避免使用任何循环,因此代码简洁明了。

import csv
from itertools import izip

with open('some.csv', 'wb') as f:
    writer = csv.writer(f)
    writer.writerows(izip(bins, frequencies))

Note that in Python 3.0 and later, you don't need izip anymore—the builtin zip now does what izip used to do.

请注意,在Python 3.0及更高版本中,您不再需要izip - 内置zip现在可以执行izip以前的操作。

更多相关文章

  1. python基础练习--列表问题
  2. 如何用所有可能的方式将一个列表分割成对?
  3. 学习python的第十六天(迭代器,三元表达式,列表生成式,字典生成式,
  4. Ansible:维护sudoers列表的最佳实践
  5. python3生成10个成绩列表,求其平均分
  6. Python列表和字典的学习
  7. Python登录并获取CSDN博客所有文章列表
  8. 在读取和评估文件列表时加速Python eval。
  9. python排序列表与铸造

随机推荐

  1. Android热修复技术链接收集
  2. Android串口通信:串口读写实例
  3. Android之---RecycleView实现简单的图片
  4. Android Fragment 真正的完全解析(上)
  5. android不同Activity之间的数据共享
  6. Android兼容性问题 -- WebP格式图片解码
  7. PHP开发Android应用程序
  8. Android 防界面劫持方案,无视Android系统
  9. 谷歌应用在Android智能机渗透率高达七成
  10. android v7兼容包RecyclerView的使用(四)—