How Can I Sort Lines And Select Some Characters Of It, Of A Text File In Python?
The lines of my text file are: << end of ENERGY. iupac_m_486_> OE1/2 will be swapped: -136.1396 1 1 openf___224_> Open Dominio1.BL00100001.pdb
Solution 1:
You need to use min
function with a proper key :
>>> min(s.split('\n\n'),key=lambda x:float(x.split()[-1])).split()[0]
'Dominio1.BL00020001.pdb'
Demo :
>>>s="""Dominio1.BL00010001.pdb 24.69530......Dominio1.BL00020001.pdb 14.33748......Dominio1.BL00030001.pdb 30.53454......Dominio1.BL00040001.pdb 23.82516......Dominio1.BL00050001.pdb 27.48684......Dominio1.BL00060001.pdb 18.17364......Dominio1.BL00070001.pdb 30.98407......Dominio1.BL00080001.pdb 17.19927......Dominio1.BL00090001.pdb 19.02460......Dominio1.BL00100001.pdb 22.57086""">>>min(s.split('\n\n'),key=lambda x:float(x.split()[-1]))
'Dominio1.BL00020001.pdb 14.33748'
>>>min(s.split('\n\n'),key=lambda x:float(x.split()[-1])).split()[0]
'Dominio1.BL00020001.pdb'
Solution 2:
A normal file read operation will do
data = file.readlines()
pdb_files = []
float_values = []
for line in data:
pdb,float_value = line.split()
pdb_files.append(pdb)
float_values.append(float(float_value))
min_float_index = float_values.indexof(min(float_values))
print pdb_files.index(min_float_index)
This code stores the data in two lists, and finds the least of the float values given. Then prints the corresponding entry of the pdb filename
Solution 3:
Try this:
defget_minimal_value_entry(file_name):
withopen(file_name, 'r') as f:%
Post a Comment for "How Can I Sort Lines And Select Some Characters Of It, Of A Text File In Python?"