This article mainly describes Python using defaultdict to read the file columns of the method, involving Python for file-related reading, traversal operation skills, the need for friends can refer to the following
This example describes how Python uses defaultdict to read each column of a file. Share to everyone for your reference, as follows:
#!/usr/bin/python "" "Usage:python *.py align_snp_site out_file" "Import Sys#import timefrom 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.set Default (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.iter Items (): Cnt2 = Counter (v) #print cnt2 result.write ("%s\t"% K) for I in Sorted (Cnt2.items (), key = Lambda x:x[1], reverse=false): Res Ult.write ("%s\t%d\t"% (i[0],i[1)) result.write ("\ n") #t1 =time.clock () #print (t1-t0)
"Recommended"
1. Special recommendation : "PHP Programmer Toolkit" V0.1 version download
2. Python Free video tutorial
3. Python Object-oriented video tutorial