Home > other >  Python code optimization
Python code optimization

Time:12-02

I self-study python small white one, born main contact letter, the task of doing now print the results is very slow,, every brother please help me to look at the code if you have any questions
If not, how can improve the running speed of the code, thank you very much!


Problem description:
[['ARG|400-C', '24.961', '47.658', '46.557'], ['ARG|400-O', '23.763', '47.387', '46.471'], ['ARG|400-CB', '25.038', '49.079', '48.617'], ['ARG|400-CG', '25.387', '49.190', '50.096'], ['ARG|400-CD', '24.513', '50.230', '50.789'], ['ARG|400-NE', '24.794', '51.592', '50.341'], ['ARG|400-CZ', '25.802', '52.335', '50.790'], ['ARG|400-NH1', '26.631', '51.851', '51.707'], ['ARG|400-NH2', '25.983', '53.562', '50.320'], ['GLU|401-N', '25.744', '47.804', '45.492'], ['GLU|401-CA', '25.224', '47.639', '44.139'], ['GLU|401-C', '24.889', '46.173', '43.874'], ['GLU|401-O', '25.182', '45.334', '44.752'], ['GLU|401-CB', '26.251', '48.128', '43.112'], ['GLU|401-CG', '26.469', '49.634', '43.114'], ['GLU|401-CD', '25.241', '50.398', '42.657'], ['GLU|401-OE1', '24.808', '50.191', '41.503'], ['GLU|401-OE2', '24.707', '51.205', '43.448']]

Elements in the list, for example, each element is a list of Numbers said the three-dimensional coordinates of the amino acids, each child element number list of [0] ', 'said a string before amino acids such as' ARG | 400 - c' and 'ARG | 400 - O said.' 'ARG | 400' that an amino acid, behind the '-' a string representation of an atom, such as' ARG | 400 - c 'said' ARG | 400 'of the amino acid c atom, I'd like to calculate all the atoms of one amino acid to the other in which all the atoms of a single amino acid three-dimensional space distance, the smallest value of all the same for the calculation, the storage for the new list, each about such format:' 1 - amino acids & gt; Amino acid 2: all elements in the amino acids and amino acids all the minimum distance between the atoms'


Hope leaders can help me solve the problem! Thank you very much!

The from math import SQRT

the from math import powImport the collections
The from numba import jit
The import numpy as np
F=open ('/Users/XRK study/Desktop/1 iir. TXT ', 'r')
R=f.r eadlines ()
F1=open ('/Users/XRK study/Desktop/1. TXT ', 'w')

F2=open ('/Users/XRK study/Desktop/1 iir_res. TXT ', 'r')
L=[]
For the line in f2:
L.A. ppend (line. The split ())
Lv=[]
For j in range (len (l)) :
Lv. Append (l [j]. [2] the replace (' : ', '))
Print (lv [10])
P=[]
For I in range (len (r)) :
If r [I] startswith (' ATOM ') :
P.a ppend (r [I]. The split ())
The else:
The continue
# print (p [10])
Def x (list, STR) :
P1=[]
For I in range (len (list)) :
If the list [I] [3] + '|' + a list [I] [5]==STR:
P1. Append (a list [I] [3] + '|' + a list [I] [5] + '-' + a list [I] [2] + ', '+ a list [I] [6] +', '+ a list [I] [7] +', '+ a list [I] [8])
Return p1
K1=[]
For I in range (len (lv)) :
K1. Append (x) (p, lv [I])
Print (k1 [10])
# below, we continue to deal with format
Tag1=[]
For I in range (len (k1) :
For j in range (len (k1) [I]) :
C=k1 [I] [j]. Replace (' : ', ', ')
D=c.s. plit (', ')
Tag1. Append (d
)
Print (tag1)
Def math_solove (list1 list2) :
Return SQRT (pow (float (list1 [1]) - float (list2 [1]), 2) + pow (float (list1 [2]) - float (list2 [2]), 2) + pow (float (list1 [3]) - float (list2 [3]), 2))

A=np. Eye (len (lv), len (lv))

Def fl (list, str1 str2) :
U=[]
For I in range (len (list)) :
For j in range (len (list)) :

If str1==str2:
The continue
The else:
If the list [I] [0] [0: the list [0]. [I] index (' - ')]==str1 and list [j] [0] [0: the list [0] [j]. J index (' - ')]==str2:
[I] u.a ppend (math_solove (list the list [j]))
Return min (u)
U1=[]
For I in range (len (lv)) :
For j in range (len (lv)) :
If I==j:
The continue
The else:
U1. Append (' {} - & gt; {}, {} 'format (lv [I], lv [j], fl (tag1, lv [I], lv [j])))
Print (u1)

CodePudding user response:

 
The import time
The from math import SQRT
The import pandas as pd

Def init () :
li=[ ['ARG|400-O', '23.763', '47.387', '46.471'], ['ARG|400-C', '24.961', '47.658', '46.557'],['ARG|400-CB', '25.038', '49.079', '48.617'], ['ARG|400-CG', '25.387', '49.190', '50.096'], ['ARG|400-CD', '24.513', '50.230', '50.789'], ['ARG|400-NE', '24.794', '51.592', '50.341'], ['ARG|400-CZ', '25.802', '52.335', '50.790'], ['ARG|400-NH1', '26.631', '51.851', '51.707'], ['ARG|400-NH2', '25.983', '53.562', '50.320'], ['GLU|401-N', '25.744', '47.804', '45.492'], ['GLU|401-CA', '25.224', '47.639', '44.139'], ['GLU|401-C', '24.889', '46.173', '43.874'], ['GLU|401-O', '25.182', '45.334', '44.752'], ['GLU|401-CB', '26.251', '48.128', '43.112'], ['GLU|401-CG', '26.469', '49.634', '43.114'], ['GLU|401-CD', '25.241', '50.398', '42.657'], ['GLU|401-OE1', '24.808', '50.191', '41.503'], ['GLU|401-OE2', '24.707', '51.205', '43.448']]
Info={}
For I in li:
AnjsName, atom_name=I [0]. The split (' - ')

Loc=[1] I
If anjsName not in info. Keys () :
The info/anjsName={' locs: (loc), 'atom_names: [atom_name]}
The else:
The info [anjsName] [' locs]. Append (loc)
The info [anjsName] [' atom_names]. Append (atom_name)
For k, v in the info. The items () :
Print (k, v)
Return the info

The class ClassAnjs () :
Def __init__ (self, locs, name, atom_names) :
"'
Initialize a amino acids
Name: param name: amino acid
: param atom_names: atomic name list
The atomic coordinates: param locs: all list
"'
The self. The name=name
Self. Otom_names=atom_names
Self. Locs=locs

Def calc_distance (self, obj) :
"'
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related