A learning Python, a small example

I, entitled:

  The amino acid sequence for a string (by the first twenty alphabet uppercase letters), it is necessary to give the number of each amino acid, and then outputted to the folder D: \ test \ frq.txt, required to achieve circulating and dictionary.

  Amino acid sequence is as follows:

  ABCDEFGHIJKLMNNLKJIHDCBA

  Examples frq.txt follows:

  The first line is the subject.

  The second line is the first letter of all twenty amino acids, to the intermediate '\ t' partition

  The third line is the frequency of occurrence of each amino acid, to the intermediate '\ t' partition

  The fourth row is the frequency of occurrence of each amino acid, to the intermediate '\ t' partition

Second, analysis

  The first is to create a dictionary. Then each dictionary is set to zero.

  Then loop through, to give the number of each letter appears.

  Then open the file (not the folder or file you need to create)

  Then is output.

Third, the code

  code show as below:

# If the directory does not exist, create 
Import OS 
dirs = ' D: \\ Test ' 
IF Not os.path.exists (dirs): 
    os.makdirs (dirs) 
# create a dictionary 
dirt1 = { ' A ' : 0 , ' B ' : 0 , ' C ' : 0 , ' D ' : 0 , ' E ' : 0 ,
              ' F. ' : 0 , ' G ' :0, 'H':0, 'I':0, 'J':0,
             'K':0, 'L':0, 'M':0, 'N':0, 'O':0,
             'P':0, 'Q' : 0 , ' R & lt ' : 0 , ' S ' : 0 , ' T ' : 0 
 } 
# where the input search character string 

STR = " MNAPERQPQPDGGDAPGHEPGGSPQDELDFSILFDYE \ 
YLNPNEEEPNAHKVASPPSGPAYPDDVLDYGLKPYSPLASL \ 
SGEPPGRFGEPDRVGPQKFLSAAKPAGASGLSPRIEITPSHE \ 
LIQAVGPLRMRDAGLLVEQPPLAGVAASPRFTLPVPGFEGYR \ 
EPLCLSPASSGSSASFISDTFSPYTSPCVSPNNGGPDDLCPQF \ 
QNIPAHYSPRTSPIMSPRTSLAEDSCLGRHSPVPRPASRSSSP \ 
GAKRRHSCAEALVALPPGASPQRSRSPSPQPSSHVAPQDHGSPA \ 
GYPPVAGSAVIMDALNSLATDSPCGIPPKMWKTSP "

# Simple Test Example 
#str = ' ABCDEFGH ' 
for X in Range (len (STR)):
      IF STR [X] in dirt1: 
         dirt1 [STR [X]] + = . 1 
# Output Display results 
#Print (dirt1) 

# output to file 
Data = Open ( ' D: \\ \\ frq.txt Test ' , ' W ' ) 
Print ( ' amino appearance frequency ' , file = Data) 
# output alphabet 
for X in dirt1: 
    Print (X + ' \ T ' , End ='' , File = Data) 
Print ( '' , File = Data) 
# number of output 
for X in dirt1: 
    Print (dirt1 [X], ' \ T ' , End = '' , File = Data) 
Print ( '' , = File Data) 
# output frequency, modified round (, . 4 ) which determine the retention of the digital to several decimal places
 for X in dirt1: 
    Print (round (dirt1 [X] / len (STR), . 4 ), ' \ T ' , End = '' , File = Data) 
data.close () 
Print ( ' Finish!')
print('you can see the result in the D:\\test\\frq.txt.')

Fourth, pay attention to the problem

  1, if the output folder does not exist, how we need to create

# If the directory does not exist, create 
Import os 
dirs = ' D: \\ the Test ' 
IF not os.path.exists (dirs): 
    os.makdirs (dirs)

  2, the document does not exist, how to create 

= Open Data ( ' D: \\ \\ frq.txt Test ' , ' W ' ) 
 // code automatically, if the document does not exist is automatically created

  3, how not to wrap output

= Open the Data ( ' D: \\ \\ frq.txt the Test ' , ' w ' ) 
// = the Data File, File keyword, data to make our own definition, represents the output to a file
// end = '', is the key, plus what our behalf after the end of the output, if only '' then ... nothing, if it is' is there is a space Print (
' name ' , end = '' , the Data File =)

  4, if the spacing between the columns to achieve the column

       The output plus '\ t' can be

  5, how to achieve output of decimal places of decimals control

     The key is round (num, 4) function, num is our operand, 4 for four decimal places.

  6, how to determine whether a key is in the dictionary.

       if x in dirt1 had enough judgment

Guess you like

Origin www.cnblogs.com/fantianliang/p/FanTianliang.html