完全初学者,如果这很明显,我很抱歉!
我有一个名称为|的文件+/-或IG_name |如此长列表中的0 -
S1 +
IG_1 0
S2 -
IG_S3 0
S3 +
S4 -
dnaA +
IG_dnaA 0
以IG_开头的所有内容都有相应的名称。我想将+或 - 添加到IG_name。例如IG_S3就像S3一样。
信息是基因名称和链信息,IG =基因间区域。基本上我想知道基因间区域在哪条链上。
我认为我想要的是:
open file
for every line, if the line starts with IG_*
find the line with *
print("IG_" and the line it found)
else
print line
我有什么:
with open(sys.argv[2]) as geneInfo:
with open(sys.argv[1]) as origin:
for line in origin:
if line.startswith("IG_"):
name = line.split("_")[1]
nname = name[:-3]
for newline in geneInfo:
if re.match(nname, newline):
print("IG_"+newline)
else:
print(line)
其中origin是混合列表,而geneInfo只有名称不是IG_names。
使用此代码,我最终会得到一个仅包含else语句的列表。
S1 +
S2 -
S3 +
S4 -
dnaA +
我的问题是我不知道搜索有什么问题,所以我可以(尝试)修复它!
答案 0 :(得分:1)
下面是一些逐步注释的代码,希望能够做你想要的(虽然不是使用print
我已经将结果聚合到一个列表中,所以你可以实际使用它)。我不太确定您的现有代码发生了什么(特别是您如何处理两个文件?)
s_dict = {}
ig_list = []
with open('genes.txt', 'r') as infile: # Simulating reading the file you pass in sys.argv
for line in infile:
if line.startswith('IG_'):
ig_list.append(line.split()[0]) # Collect all our IG values for later
else:
s_name, value = line.split() # Separate out the S value and its operator
s_dict[s_name] = value.strip() # Add to dictionary to map S to operator
# Now you can go back through your list of IG values and append the appropriate operator
pulled_together = []
for item in ig_list:
s_value = item.split('_')[1]
# The following will look for the operator mapped to the S value. If it is
# not found, it will instead give you 'not found'
corresponding_operator = s_dict.get(s_value, 'Not found')
pulled_together.append([item, corresponding_operator])
print ('List structure')
print (pulled_together)
print ('\n')
print('Printout of each item in list')
for item in pulled_together:
print(item[0] + '\t' + item[1])
答案 1 :(得分:0)
nname = name[:-3]
Python通过列表切片非常强大,但要正确理解可能很棘手。
当您写[:-3]时,除最后三项外,您将获取所有内容。问题是,如果列表中的元素少于三个,则不会返回错误,而是返回空列表。
我认为这是事情不起作用的地方,因为每行没有太多元素,它会返回一个空列表。如果你能说出你究竟希望它返回那里,通过一个例子或其他东西,它会有很大的帮助,因为我真的不知道你想要用你的切片得到什么。
答案 2 :(得分:0)
这样做你想要的吗?
from __future__ import print_function
import sys
# Read and store all the gene info lines, keyed by name
gene_info = dict()
with open(sys.argv[2]) as gene_info_file:
for line in gene_info_file:
tokens = line.split()
name = tokens[0].strip()
gene_info[name] = line
# Read the other file and lookup the names
with open(sys.argv[1]) as origin_file:
for line in origin_file:
if line.startswith("IG_"):
name = line.split("_")[1]
nname = name[:-3].strip()
if nname in gene_info:
lookup_line = gene_info[nname]
print("IG_" + lookup_line)
else:
pass # what do you want to do in this case?
else:
print(line)