简体   繁体   English

打印元素不在列表中

[英]Printing elements out of list

I have a certain check to be done and if the check satisfies, I want the result to be printed. 我有一定的检查要完成,如果检查满意,我希望打印结果。 Below is the code: 下面是代码:

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

The above code works great but when I print j, it prints like ['351', '342'] but I am expecting to get 351 342 (with one space in between). 上面的代码很好用,但是当我打印j时,它打印的像['351','342'],但是我希望得到351342(中间有一个空格)。 Since it is more of a python question, I have not included the input files (basically they are just numbers). 由于它更多是python问题,因此我没有包含输入文件(基本上它们只是数字)。

Can anyone help me? 谁能帮我?

Cheers, 干杯,

Chavanak Chavanak

To convert a list of strings to a single string with spaces in between the lists's items, use ' '.join(seq) . 要将字符串列表转换为单个字符串,且列表项之间有空格,请使用' '.join(seq)

>>> ' '.join(['1','2','3'])
'1 2 3'

You can replace ' ' with whatever string you want in between the items. 您可以在项目之间使用所需的任何字符串替换' '

Mark Rushakoff seems to have solved your immediate problem, but there are some other improvements that could be made to your code. Mark Rushakoff似乎已经解决了您的迫在眉睫的问题,但是您的代码还可以进行其他一些改进。

  • Always use context managers ( with open(filename, mode) as f: ) for opening files rather than relying on close getting called manually. 始终使用上下文管理器( with open(filename, mode) as f:来打开文件,而不是依靠close手动调用。
  • Don't bother reading a whole file into memory very often. 不要经常将整个文件读入内存。 Looping over some_file.readilines() can be replaced with looping over some_file directly. 循环some_file.readilines()可以替换为直接循环some_file

    • For example, you could have used map(string.strip, ligland_file) or better yet [line.strip() for line in ligland_file] 例如,您本可以使用map(string.strip, ligland_file)或更好的方法[line.strip() for line in ligland_file]
  • Don't choose names to include the type of the object they refer to. 不要选择名称来包括它们所引用的对象的类型。 This information can be found other ways. 可以通过其他方式找到此信息。

For exmaple, the code you posted can be simplified to something along the lines of 例如,您发布的代码可以简化为以下形式:

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)

which is more reliable and I believe more easily read. 这更可靠,我相信更容易阅读。

Note that the algorithmic performance of this is far from ideal because of these nested loops. 注意,由于这些嵌套循环,其算法性能远非理想。 Depending on your need, this could potentially be improved, but since I don't know exactly what data you need to extract to tell you whether you can. 根据您的需要,这可能会有所改善,但是由于我不知道确切需要提取哪些数据才能告诉您是否可以。

The time this takes currently in my code and yours is O(n m q), where n is the number of lines in one file, m is the number of lines in the other, and q is the length of lines in unique_count_c_from_ac.txt. 当前在我的代码中花费的时间是O(n m q),其中n是一个文件中的行数,m是另一个文件中的行数,q是unique_count_c_from_ac.txt中的行长。 If two of these are fixed/small, then you have linear performance. 如果其中两个是固定的/较小的,那么您将具有线性性能。 If two can grow arbitrarily (I sort of imagine n and m can?), then you could look into improving your algorithm, probably using sets or dicts. 如果两个可以任意增长(我可以想象n和m可以?),那么您可以考虑使用集合或字典来改进算法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM