我正在从这个格式转换文件:加入不同长度的字符串在一个循环
# SampleNamea seq1a seq2a
# SampleNameb seq1b seq2b
# SampleNamec seq1c seq2c
# SampelNamed seq1d seq2d
为此格式:
# SampleNamea SampleNameb 0 0 0 0 s s e e q q 1 1 a b s s e e q q 2 2 a b
# SampleNamec SampleNamed 0 0 0 0 s s e e q q 1 1 c d s s e e q q 2 2 c d
目前剧本我如果seq1a
作品,seq1b
等长度相同。但在数据集中,我有不同的字符串长度。如果我尝试在我的数据集上运行脚本,我会收到消息IndexError: string index out of range
。
这是脚本的所述部分:附图出字符串的长度(即seq1aseq2a
,seq1bseq2b
),将其追加到InputMasterList
,加SampleName
s的额外的零的给OutputMasterList
。然后,通过从InputMasterList[LineEven]
字符串(seq1aseq2a
)和InputMasterList[LineOdd]
字符串(seq1bseq2b
)中选择以元素[0]开始的每个连续元素并将它们组合到OutputMasterList
中,从而将字符串附加到OutputMasterList
。所以结果将是(s s e e q q 1 1 a b s s e e q q 2 2 a b
)。
我怎样才能让这个脚本在不同的字符串长度上工作?
LineEven = 0
LineOdd = 1
RecordNum = 1
while RecordNum < (NumofLinesInFile/2):
for i in range(len(InputMasterList[LineEven])):
if i == 0:
OutputMasterList.append(SampleList[LineEven]+'\t'+ SampleList[LineEven]+'\t'+'0'+'\t'+'0'+'\t'+'0'+'\t'+'0'+'\t')
OutputMasterList[RecordNum] = InputMasterList[LineEven][i]+'\t'+InputMasterList[LineOdd][i]+'\t'
RecordNum = RecordNum + 1
LineEven = LineEven + 2
LineOdd = LineOdd + 2
我非常初学者,所以我知道这个代码是相当繁琐的,但任何帮助,将不胜感激。如果你需要澄清我想要用这个脚本做什么,请不要犹豫,问。
更新:感谢您的及时回复。由于您的反馈,我意识到我必须改变我的问题的性质。在我的数据集中,我缺少了我的脚本不喜欢的序列,我需要用占位符来解决这个缺失的数据,这个占位符的长度与对应的长度相同。
旧格式:
# SampleNamea seq1a seq2a
# SampleNameb '.' seq2b
新格式:
# SampleNamea seq1a seq2a
# SampleNameb NNNNN seq2b
然后,我相信我的脚本将工作!
TL; DR - 根据您的反馈,我有基于我的下一步应该是什么。
你能确定你的缩进是正确的吗?现在有一个无限循环,因为'RecordNum'在while循环内部不会增加 – Greg
您的示例没有说明如何更改格式。请从旧的口头描述新的格式。 –
如果seq1asequence2a的长度不同,那么输出结果如何? –