Code instances in Python with defaultdict for each column of the file

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.