小编for*_*unk的帖子

避免嵌套两个for循环

请看下面的代码:

import string
from collections import defaultdict



first_complex=open( "residue_a_chain_a_b_backup.txt", "r" )
first_complex_lines=first_complex.readlines()
first_complex_lines=map( string.strip, first_complex_lines )
first_complex.close()

second_complex=open( "residue_a_chain_a_c_backup.txt", "r" )
second_complex_lines=second_complex.readlines()
second_complex_lines=map( string.strip, second_complex_lines )
second_complex.close()
list_1=[]
list_2=[]
for x in first_complex_lines:
    if x[0]!="d":
        list_1.append( x )
for y in second_complex_lines:
    if y[0]!="d":
        list_2.append( y ) 
j=0
list_3=[]      
list_4=[]
for a in list_1:
    pass
    for b in list_2:
        pass
        if a==b:
            list_3.append( a )    

kvmap=defaultdict( int )
for k in list_3:
    kvmap[k]+=1 
print kvmap
Run Code Online (Sandbox Code Playgroud)

通常我使用izip或izip_longest来为两个循环播放,但这次文件的长度是不同的.我不想要一个无条目.如果我使用上述方法,则运行时间变为增量且无用.我怎么能让两个for循环去?

干杯,Chavanak

python for-loop

2
推荐指数
1
解决办法
1694
查看次数

打印元素不在列表中

我有一个检查要完成,如果检查满足,我想要打印结果.以下是代码:

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 …
Run Code Online (Sandbox Code Playgroud)

python printing list

1
推荐指数
1
解决办法
8595
查看次数

标签 统计

python ×2

for-loop ×1

list ×1

printing ×1