[英]Printing elements out of list
我有一定的檢查要完成,如果檢查滿意,我希望打印結果。 下面是代碼:
import string
import codecs
import sys
y=sys.argv[1]
list_1=[]
f=1.0
x=0.05
write_in = open ("new_file.txt", "w")
write_in_1 = open ("new_file_1.txt", "w")
ligand_file=open( y, "r" ) #Open the receptor.txt file
ligand_lines=ligand_file.readlines() # Read all the lines into the array
ligand_lines=map( string.strip, ligand_lines ) #Remove the newline character from all the pdb file names
ligand_file.close()
ligand_file=open( "unique_count_c_from_ac.txt", "r" ) #Open the receptor.txt file
ligand_lines_1=ligand_file.readlines() # Read all the lines into the array
ligand_lines_1=map( string.strip, ligand_lines_1 ) #Remove the newline character from all the pdb file names
ligand_file.close()
s=[]
for i in ligand_lines:
for j in ligand_lines_1:
j = j.split()
if i == j[1]:
print j
上面的代碼很好用,但是當我打印j時,它打印的像['351','342'],但是我希望得到351342(中間有一個空格)。 由於它更多是python問題,因此我沒有包含輸入文件(基本上它們只是數字)。
誰能幫我?
干杯,
Chavanak
要將字符串列表轉換為單個字符串,且列表項之間有空格,請使用' '.join(seq)
。
>>> ' '.join(['1','2','3'])
'1 2 3'
您可以在項目之間使用所需的任何字符串替換' '
。
Mark Rushakoff似乎已經解決了您的迫在眉睫的問題,但是您的代碼還可以進行其他一些改進。
with open(filename, mode) as f:
來打開文件,而不是依靠close
手動調用。 不要經常將整個文件讀入內存。 循環some_file.readilines()
可以替換為直接循環some_file
。
map(string.strip, ligland_file)
或更好的方法[line.strip() for line in ligland_file]
不要選擇名稱來包括它們所引用的對象的類型。 可以通過其他方式找到此信息。
例如,您發布的代碼可以簡化為以下形式:
import sys
from contextlib import nested
some_real_name = sys.argv[1]
other_file = "unique_count_c_from_ac.txt"
with nested(open(some_real_name, "r"), open(other_file, "r")) as ligand_1, ligand_2:
for line_1 in ligand_1:
# Take care of the trailing newline
line_1 = line_1.strip()
for line_2 in ligand_2:
line_2 = line2.strip()
numbers = line2.split()
if line_1 == numbers[1]:
# If the second number from this line matches the number that is
# in the user's file, print all the numbers from this line
print ' '.join(numbers)
這更可靠,我相信更容易閱讀。
注意,由於這些嵌套循環,其算法性能遠非理想。 根據您的需要,這可能會有所改善,但是由於我不知道確切需要提取哪些數據才能告訴您是否可以。
當前在我的代碼中花費的時間是O(n m q),其中n是一個文件中的行數,m是另一個文件中的行數,q是unique_count_c_from_ac.txt中的行長。 如果其中兩個是固定的/較小的,那么您將具有線性性能。 如果兩個可以任意增長(我可以想象n和m可以?),那么您可以考慮使用集合或字典來改進算法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.