首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在python中使用"-“?

如何在python中使用"-“?
EN

Stack Overflow用户
提问于 2014-09-16 09:56:41
回答 3查看 90关注 0票数 0

所以这段python代码是给我的。这暗示着它起作用了,但很明显我有问题。我想知道有没有人能告诉我出了什么问题?

当我尝试运行时,错误总是发生

代码语言:javascript
复制
 intron1_length = ((my_dna.find(‘gtcata’)) –
 (my_dna.find(‘atcgat’)))

错误是"-“。只是不确定应该用什么来代替。谢谢!

代码语言:javascript
复制
 # -*- coding: utf-8 -*-

 # We’ll start with “data-aware” way to do this first.
 # Since we have the sequence, we can see that exon 1 ends with
 # ‘GACTA’. We can use that information, and that method, for
 # finding the positions of the elements of this gDNA. This is NOT
 # an elegant solution, and would not work if we didn’t know the
 # DNA sequence ahead of time.

 my_dna = "ATCGATCGATGGTCGAATGACTAgtcatagctatgcatgtagctactc
 gatcgtattttattcgatcgatcgatCGATCGATCATGCTATCATCGATCGATATCGATGCATC
 GACTACTATgtcatggctatgcatcgatcgtattttattcgatcgttcgatGATCGATCGATCGACTGACTTTGAA"   
 # here we introduce another useful operator for strings: len
 gene_length = len(my_dna)
 # we’ll use the starting position of useful substrings in the
 # sequence to find the positions of the exon-intron boundaries.
 # We’ll then use those to find the length of each segment.
 exon1_length = (my_dna.find('Agtcata'))
 intron1_length =((my_dna.find('gtcata')) - (my_dna.find('atcgat')))
 exon2_length = ((my_dna.find('CGATCG'))- (my_dna.find('Tgtcatg')))
 intron2_length = ((my_dna.find('gtcatg'))- (my_dna.find('tGATCGA')))
 exon3_length = (gene_length(my_dna.find('GATCGA'))
 print ("Gene length:" + str(gene_length))
 print ("Exon1 length:" + str(exon1_length))
 print ("Intron1 length:" + str(intron1_length))
 print ("Exon2 length:" + str(exon2_length))
 print ("Intron2 length:" + str(intron2_length))
 print ("Exon2 length:" + str(exon3_length)) 
EN

回答 3

Stack Overflow用户

发布于 2014-09-16 10:07:32

因为 != -

你用什么来编辑你的代码呢?文字处理器??

这些引述看起来也很可疑。使用不会将这些放入代码中的编辑器。

票数 5
EN

Stack Overflow用户

发布于 2014-09-16 10:07:43

下面的代码运行时没有错误:

代码语言:javascript
复制
my_dna = """ATCGATCGATGGTCGAATGACTAgtcatagctatgcatgtagctactc
gatcgtattttattcgatcgatcgatCGATCGATCATGCTATCATCGATCGATATCGATGCATC
GACTACTATgtcatggctatgcatcgatcgtattttattcgatcgttcgatGATCGATCGATCGACTGACTTTGAA"""
# here we introduce another useful operator for strings: len
gene_length = len(my_dna)
# we’ll use the starting position of useful substrings in the
# sequence to find the positions of the exon-intron boundaries.
# We’ll then use those to find the length of each segment.
exon1_length = my_dna.find('Agtcata')
intron1_length = my_dna.find('gtcata') - my_dna.find('atcgat')
exon2_length = my_dna.find('CGATCG') - my_dna.find('Tgtcatg')
intron2_length = my_dna.find('gtcatg') - my_dna.find('tGATCGA')
exon3_length = gene_length - my_dna.find('GATCGA')
print ("Gene length:" + str(gene_length))
print ("Exon1 length:" + str(exon1_length))
print ("Intron1 length:" + str(intron1_length))
print ("Exon2 length:" + str(exon2_length))
print ("Intron2 length:" + str(intron2_length))
print ("Exon2 length:" + str(exon3_length))

假设您使用的是Python3,我将括号保留在打印语句中。

要使代码运行,只需要更改两个命令:

  • my_dna的定义是一个多行字符串,因此它需要三个引号。另一方面,如果你希望它是一个单行字符串,那么就把它全部放在一行上。
  • exon3_length行有两个问题:不平衡的括号和调用整数的尝试。

在修复这些问题之后,代码就会运行。

不需要更改一个即可运行代码。

票数 1
EN

Stack Overflow用户

发布于 2014-09-16 10:16:19

这是代码中出错的部分:

exon3_length = (gene_length(my_dna.find('GATCGA'))

前面通过在gene_length字符串上调用len将dna定义为整数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25859577

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档