0

我有这两个功能:

def MatchRNA(RNA, start1, end1, start2, end2):
    Subsequence1 = RNA[start1:end1+1]
    if start1 > end1:
        Subsequence1 = RNA[start1:end1-1:-1]
    Subsequence2 = RNA[start2:end2+1]
    if start2 > end2:
        Subsequence2 = RNA[start2:end2-1:-1]
    return Subsequence1, Subsequence2


def main():
    RNA_1_list = ['A','U','G','U','G','G','G','U','C','C','A','C','G','A','C','U','C','G','U','C','G','U','C','U','A','C','U','A','G','A']
    RNA_2_list = ['C','U','G','A','C','G','A','C','U','A','U','A','A','G','G','G','U','C','A','A','G','C']
    RNA_Values = {'A': 1, 'U': 2, 'C': 3, 'G': 4}
    RNA1 = []
    RNA2 = []
    for i in RNA_1_list:
        if i in RNA_Values:
            RNA1.append(RNA_Values[i])
    for i in RNA_2_list:
        if i in RNA_Values:
            RNA2.append(RNA_Values[i])
    RNA = list(input("Which strand of RNA (RNA1 or RNA2) are you sequencing? "))       
    Start1, End1, Start2, End2 = eval(input("What are the start and end values (Start1, End1, Start2, End2) for the subsequences of the strand? "))
    Sub1, Sub2 = MatchRNA(RNA, Start1, End1, Start2, End2)
    print(Sub1)
    print(Sub2)

因此,当我运行 main 函数并给出输入(例如):RNA1,然后是 3、14、17、28 时,它应该打印两个列表,[2,4,4,4,2,3,3,1,3,4,1,3]并且[4,2,3,4,2,3,2,1,3,2,1,4]. 我在测试这段代码时无意中使用了 Python 2.7,它运行良好(没有那个 eval),但是当我在 3.3 中运行它(并将 eval 放回)它打印两个列表,['1']和 []。有谁知道为什么它在 3.3 中不起作用或者我如何让它在 3.3 中起作用?提前致谢。

4

2 回答 2

3

input()在 Python3 中返回一个字符串,而在 Python2 中它等价于eval(raw_input).

作为RNA1输入:

Python3:

>>> RNA1 = []
>>> list(input("Which strand of RNA (RNA1 or RNA2) are you sequencing? "))
Which strand of RNA (RNA1 or RNA2) are you sequencing? RNA1
['R', 'N', 'A', '1']

Python2:

>>> list(eval(input("Which strand of RNA (RNA1 or RNA2) are you sequencing? ")))
Which strand of RNA (RNA1 or RNA2) are you sequencing? RNA1
[]
于 2013-11-02T20:24:57.610 回答
0

关闭,但问题的主要来源在这里:

 RNA = list(input("Which strand of RNA (RNA1 or RNA2) are you sequencing? "))

在任何情况下,那里的list()电话都是无用的。

在 Python2 中,当您输入 时RNA1input()计算该符号并返回绑定到 的列表RNA1

在 Python3 中,input()返回字符串 "RNA1",无意义的 ;-)list()变成

['R', 'N', 'A', '1']

更改代码以在两个版本下运行的一种方法:首先在顶部添加:

try:
    raw_input
except:  # Python 3
    raw_input = input

然后更改输入行:

RNA = eval(raw_input("Which strand of RNA (RNA1 or RNA2) are you sequencing? "))
Start1, End1, Start2, End2 = eval(raw_input("What are the start and end values (Start1, End1, Start2, End2) for the subsequences of the strand? "))
于 2013-11-02T20:43:54.220 回答