Python使用defaultdict读取文件各列的方法
作者:盛意兄弟 时间:2021-08-05 00:34:24
本文实例讲述了Python使用defaultdict读取文件各列的方法。分享给大家供大家参考,具体如下:
#!/usr/bin/python
"""USAGE: python *.py align_SNP_site out_file"""
import sys
#import time
from collections import Counter
#t0=time.clock()
info=open(sys.argv[1])
fast=sys.argv[2]
d_c = {}
d1={}
d2={}
for line in info:
cols=line.strip().split("\t")
if cols[0] == "SNP pattern":
continue
else:
d1.setdefault(cols[4],[]).append(cols[1])
d2.setdefault(cols[7],[]).append(cols[1])
#d1.setdefault(cols[0],[]).append(cols[5])
#d2[cols[0]] = "\t".join(cols[0:3])
info.close()
print len(d1)
print len(d2)
my_list=[]
ref_fa = open("some_example.fasta", 'r')
for i in ref_fa.readlines():
if i.startswith(">"):
my_list.append(i.rstrip())
ref_fa.close()
print len(my_list)
#sys.exit()
result = open(fast,'w')
for k,v in d1.iteritems():
cnt1 = Counter(v)
#print cnt1
result.write("%s\t" % k)
for i in sorted(cnt1.items(), key = lambda x: x[1], reverse=True):
result.write("%s\t%d\t"%(i[0],i[1]))
result.write("\n")
for k,v in d2.iteritems():
cnt2 = Counter(v)
#print cnt2
result.write("%s\t" % k)
for i in sorted(cnt2.items(), key = lambda x: x[1], reverse=False):
result.write("%s\t%d\t"%( i[0],i[1]))
result.write("\n")
#t1=time.clock()
#print (t1-t0)
希望本文所述对大家Python程序设计有所帮助。
标签:Python,读取文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python使用 TCP协议实现智能聊天机器人功能
2022-08-19 03:49:48
![](https://img.aspxhome.com/file/2023/4/123694_0s.png)
Ubuntu下创建虚拟独立的Python环境全过程
2021-07-27 13:22:29
Mysql中explain的使用详解
2009-12-08 16:18:00
asp 实现当有新信息时播放语音提示的效果
2011-03-31 11:00:00
python实现单机五子棋对战游戏
2022-01-11 04:38:10
![](https://img.aspxhome.com/file/2023/9/65909_0s.jpg)
python在windows调用svn-pysvn的实现
2022-03-15 05:13:48
python中多个装饰器的执行顺序详解
2022-04-30 13:43:51
![](https://img.aspxhome.com/file/2023/9/112459_0s.png)
浅谈keras的深度模型训练过程及结果记录方式
2023-08-11 03:17:46
利用global.asa计划执行程序
2008-03-05 12:49:00
python-docx如何缩进两个字符
2022-07-04 15:56:45
![](https://img.aspxhome.com/file/2023/5/124925_0s.png)
MySQL 数据库语句优化的原则
2010-01-20 10:11:00
Python+pytorch实现天气识别
2021-09-23 18:49:13
![](https://img.aspxhome.com/file/2023/6/79566_0s.png)
django实现同一个ip十分钟内只能注册一次的实例
2021-03-07 03:13:37
Python中模块的使用--binascii模块用法
2022-11-16 13:53:16
Oracle 10g的DBA无法登录解决方案
2009-05-24 19:38:00
python儿童学游戏编程知识点总结
2022-10-23 04:32:42
Python3.4实现远程控制电脑开关机
2022-09-17 06:19:59
适宜做简单搜索的MySQL数据库全文索引
2009-01-04 13:11:00
python进程池的简单实现
2021-02-22 06:22:14
python每天定时运行某程序代码
2023-11-03 07:03:52