gpt4 book ai didi

python-3.x - 从 __main__ 保存列表

转载 作者:行者123 更新时间:2023-12-04 17:05:45 24 4
gpt4 key购买 nike

确定的解决方案:应该注意,它应该像迭代它以附加到列表一样简单......但是,应该指出的是,为什么这对我不起作用的大问题是我正在使用和IDE(Spyder 4.1.3),当我运行代码时,它没有执行我想要的输出。但是,如果我将其保存到 py 脚本中并执行,则通过迭代附加数据不会出现问题...

结论是,如果使用 IDE 并通过执行整个脚本来运行问题测试以排除 IDE 错误。

我有一个类,我想用它来解析一些数据。我成功地使用了它并创建了一个变量来保存这个类的输出。我可以使用 for 循环来查看解析的数据,但我无法将它们保存到实际列表中。有人可以帮我把这些数据拿出来吗?

Class: 

You can use this code and put it in your own script
class ParseFastQ(object):
"""Returns a read-by-read fastQ parser analogous to file.readline()"""
def __init__(self,filePath,headerSymbols=['@','+']):
"""Returns a read-by-read fastQ parser analogous to file.readline().
Exmpl: parser.__next__()
-OR-
Its an iterator so you can do:
for rec in parser:
... do something with rec ...
rec is tuple: (seqHeader,seqStr,qualHeader,qualStr)
"""
if filePath.endswith('.gz'):
self._file = gzip.open(filePath)
else:
self._file = open(filePath, 'rU')
self._currentLineNumber = 0
self._hdSyms = headerSymbols
def __iter__(self):
return self

def __next__(self):
"""Reads in next element, parses, and does minimal verification.
Returns: tuple: (seqHeader,seqStr,qualHeader,qualStr)"""
# ++++ Get Next Four Lines ++++
elemList = []
for i in range(4):
line = self._file.readline()
self._currentLineNumber += 1 ## increment file position
if line:
elemList.append(line.strip('\n'))
else:
elemList.append(None)

# ++++ Check Lines For Expected Form ++++
trues = [bool(x) for x in elemList].count(True)
nones = elemList.count(None)
# -- Check for acceptable end of file --
if nones == 4:
raise StopIteration
# -- Make sure we got 4 full lines of data --
assert trues == 4,\
"** ERROR: It looks like I encountered a premature EOF or empty line.\n\
Please check FastQ file near line number %s (plus or minus ~4 lines) and try again**" % (self._currentLineNumber)
# -- Make sure we are in the correct "register" --
assert elemList[0].startswith(self._hdSyms[0]),\
"** ERROR: The 1st line in fastq element does not start with '%s'.\n\
Please check FastQ file near line number %s (plus or minus ~4 lines) and try again**" % (self._hdSyms[0],self._currentLineNumber)
assert elemList[2].startswith(self._hdSyms[1]),\
"** ERROR: The 3rd line in fastq element does not start with '%s'.\n\
Please check FastQ file near line number %s (plus or minus ~4 lines) and try again**" % (self._hdSyms[1],self._currentLineNumber)
# -- Make sure the seq line and qual line have equal lengths --
assert len(elemList[1]) == len(elemList[3]), "** ERROR: The length of Sequence data and Quality data of the last record aren't equal.\n\
Please check FastQ file near line number %s (plus or minus ~4 lines) and try again**" % (self._currentLineNumber)

# ++++ Return fatsQ data as tuple ++++
return tuple(elemList)

#

调用这个类来创建变量:
fastqfile=ParseFastQ('filepath')

然后我想确定它是什么类,因为它不允许我提取解析的 fasqdata
In: type(fastqfile)
Out: __main__.ParseFastQ

这部分代码会让我打印所有输出
for fastq_obj in fastqfile:
#This is the header
print(fastq_obj[0])

我试图提取该数据并保存在列表中...
seqHeader=[]
for fastq_obj in fastqfile:
#This is the header
seqHeader.append(print(fastq_obj[0]))

我也尝试了 .extend 并没有保存任何内容

任何帮助,将不胜感激

最佳答案

试试这个 :

seqHeader=[]
for fastq_obj in fastqfile:
#This is the header
seqHeader.append(fastq_obj[0])

# look at the seqHeader list
print(seqHeader)

我真的不明白你为什么通过 print() func 内追加。 print() func 不返回任何内容,因此列表中不能附加任何内容。

关于python-3.x - 从 __main__ 保存列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62043501/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com