使用3行作为值创建字典

2024-10-04 11:29:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我有要解析的fastq文件。下面显示了每个文件中“读取”数千次的示例:

@PSI179204_0037:4:1:2139:945#0/2
AGAGATCCTACGGGAGGCAGCAGTGAGGAATATTGGTCAATGGGCGCGAGCCTGAACCAGCCAAGTAGCGTGAGGGACGACTGCCCTACGGGTTGTAAACCTCTTTTGTTCGGGAATAAAGTGCGGCACGCGTGCCGGTTTGTATGTCCCGTTCGAATAG
+PSI179204_0037:4:1:2139:945#0/2
ghhhhhhhhhhhfhdhhhfhhhhhgeeghhhdghfgheh[hhfhfhhhhehghffcahhhhfgcfgeaegd_ah_aaOa[a[aW___W^`a`b`da`ZXO]N^``BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB^C

我的目的是让他们在字典里,如下图所示,每一行都被缩短了:

{'  @PSI179204_0037:4': 'AGAGATCCTACG' '+PSI179204_0' 'ghhhhhhhhh' }

我在这里看到,您可以将一行声明为键,然后使用next(filename)命令将next行用作值,因此尝试使用此命令,但有3个next(filename)条目,如下代码所示:

file1 = open(inputfile1, 'r')
file2 = open(inputfile2, 'r')
File1dict = {}
File2dict = {}



for key in file1:
    File1dict[key.strip()] = next(file1) = next(file1) = next(file1)
    print (File1dict)

for key in file2:
    File2dict[key.strip()] = next(file2) = next(file2) = next(file2)    
    print (File2dict)

目前我得到以下错误:

  File "Dict_maybesworking.py", line 31
    File1dict[key.strip()] = next(file1) = next(file1) = next(file1)
SyntaxError: can't assign to function call

是否有人知道如何使这个代码工作,如果不是一个替代品?你知道吗

全文如下:

from __future__ import print_function
from collections import defaultdict
from itertools import groupby
import argparse 
from itertools import izip

parser = argparse.ArgumentParser() #simplifys the wording of using argparse as stated in the python tutorial
parser.add_argument("-r1", type=str, action='store',  dest='input1', help="input the forward read file") # allows input of the forward read
parser.add_argument("-r2", type=str, action='store', dest='input2', help="input the reverse read file") # allows input of the reverse read
parser.add_argument("-v", "--verbose", action="store_true", help=" Increases the output, only needs to be used to provide feedback for debugging")
parser.add_argument("-u", type=str, action='store', dest='unique', help="Unique insturment number for fastq file required") # allows input of the reverse read
parser.add_argument("-n", action="count", default=0, help="Allows for up to 5 mismatches, Default is 0")
parser.add_argument("-o", "--output", help="Directs the output to a name of your choice")
args = parser.parse_args()

Uni = str(args.unique)
inputfile1 = str(args.input1)
inputfile2 = str(args.input2)
output = str(args.output)  
output_file= open(output, "w")
Unmatched_1 = open('Unmatched_1', "a")
Unmatched_2 = open('Unmatched_2', "a")
file1 = open(inputfile1, 'r')
file2 = open(inputfile2, 'r')
File1dict = {}
File2dict = {}



for key in file1:
    File2dict[key.strip()] = [file2.next(), file2.next(), file2.next()]
    print (File1dict)

for key in file2:
    File2dict[key.strip()] = [file2.next(), file2.next(), file2.next()] 
    print (File2dict)

命令行使用:

python Dict_maybesworking.py -r1 Real_test_1 -r2 Real_test_2 -u PSI179204 -o file_result

Tags: thekeyaddparserforoutputhelpopen
2条回答

你没有正确使用next方法,你必须把你的新值放在列表中或者其他地方 其他数据结构:

File1dict[key.strip()] = [file1.next(), file1.next(), file1.next()]
....
File2dict[key.strip()] = [file2.next(), file2.next(), file2.next()]  

由于文件对象是iterable,您可以像现在一样迭代以获取键,然后从同一iterable中切片接下来的3次出现以获取值,例如:

from itertools import islice

with open('file1') as fin:
    stripped_lines = (line.strip() for line in fin)
    f1dict = {key: list(islice(stripped_lines, 3)) for key in stripped_lines}

注意,for line in fin一次消耗一行,但是list(islice(fin, 3))然后消耗来自fin的3行,因此下一个for然后消耗之后的行,以此类推。。你知道吗

例如:

>>> from itertools import islice
>>> r = range(20)
>>> i = iter(r)
>>> {key: list(islice(i, 3)) for key in i}
{0: [1, 2, 3], 8: [9, 10, 11], 4: [5, 6, 7], 12: [13, 14, 15], 16: [17, 18, 19]}

相关问题 更多 >