如果列表索引超出范围python错误,则赋值变量

时间:2023-01-16 16:39:23

How to pass a string to a variable if an index error is found? Consider the code:

如果找到索引错误,如何将字符串传递给变量?考虑一下代码:

for l1, l2 in zip(open('file1.list'), open ('file2.list')):
  a=fasta1[int(l1)]
  b=fasta2[int(l2)]

  alignments = pairwise2.align.globalxx(a,b)
  top_aln = alignments[0]
  aln_a, aln_b, score, begin, end = top_aln
  print aln_a+'\n'+aln_b
  outfast1 = aln_a
  outfast2 = aln_b

A number of these functions must be imported (pairwise2 align), but the file.lists are single column text files with one sequence id (text and numbers) per line, that are used to extract from the fasta1 and fasta2 text files.

必须导入许多这些函数(pairwise2 align),但file.lists是单行文本文件,每行有一个序列id(文本和数字),用于从fasta1和fasta2文本文件中提取。

Basically, I want to try: each list command ( a=fasta1[int(l1)]) and if there is no error (the id is in range), do as normal (assign variables a and b for that iteration), but if NOT, assign the 'a' variable some placeholder text like 'GGG':

基本上,我想尝试:每个列表命令(a = fasta1 [int(l1)]),如果没有错误(id在范围内),正常做(为该迭代分配变量a和b),但如果不是,请为'a'变量指定一些占位符文本,例如'GGG':

for l1, l2 in zip(open('file1.list'), open ('file2.list')):
 try:
  a=fasta1[int(l1)]
 except IndexError,e:
  a="GGG"
 continue

 try:
  b=fasta2[int(l2)]
 except (IndexError):
  b="CCC"
 continue

This code doesn't quite work (when integrated with above code), which isn't surprising given my lack of python prowess, but I don't quite know why. I actually get no text output, despite the print calls... Am I thinking about this right? If there is NO error in the index, I just want it to go on and do the pairwise alignment (with the first a and b variables) and then print some text to stdout.

这段代码不太合适(当与上面的代码集成时),这并不奇怪,因为我缺乏python实力,但我不知道为什么。尽管有打印电话,我实际上没有输出任何文字......我是否正在思考这个问题?如果索引中没有错误,我只想让它继续进行成对对齐(使用第一个a和b变量),然后将一些文本打印到stdout。

Any ideas?

有任何想法吗?

2 个解决方案

#1


1  

Python's conditional (aka ternary) expressions can one-line this for you. They're often criticized for lack of readability, but I think this example reads well enough.

Python的条件(也称为三元)表达式可以为您排除这一点。他们经常因缺乏可读性而受到批评,但我认为这个例子读得很好。

a = fasta1[int(l1)] if int(l1) < len(fasta1) else "GGG"

#2


0  

You don't need continue, because it will skip that iteration of the loop. Consider the following:

您不需要继续,因为它将跳过循环的迭代。考虑以下:

for l1, l2 in zip(open('file1.list'), open ('file2.list')):
 a = 'GGG'
 b = 'CCC'
 try:
  a = fasta1[int(l1)]
  b = fasta2[int(l2)]
 except IndexError:
  pass

#1


1  

Python's conditional (aka ternary) expressions can one-line this for you. They're often criticized for lack of readability, but I think this example reads well enough.

Python的条件(也称为三元)表达式可以为您排除这一点。他们经常因缺乏可读性而受到批评,但我认为这个例子读得很好。

a = fasta1[int(l1)] if int(l1) < len(fasta1) else "GGG"

#2


0  

You don't need continue, because it will skip that iteration of the loop. Consider the following:

您不需要继续,因为它将跳过循环的迭代。考虑以下:

for l1, l2 in zip(open('file1.list'), open ('file2.list')):
 a = 'GGG'
 b = 'CCC'
 try:
  a = fasta1[int(l1)]
  b = fasta2[int(l2)]
 except IndexError:
  pass