"Value Error" in Python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AuroraBorealis
    New Member
    • Oct 2009
    • 4

    "Value Error" in Python

    Hello, I'm trying to write this simple program that returns True if two equal-length strings are complimentary DNA pairs (ie, if character 1 in string 1 was 'A', character 1 in string 2 has to be 'T', and vice-versa, otherwise it would return false).
    This is what I have so far:
    Code:
    [B]for[/B] (base1, base2) [B]in [/B](strand1, strand2):
        [B]if[/B] (base1, base2) [B]not in[/B] (('A', 'T'), ('T', 'A'), ('C', 'G'), ('G', 'C')):
            [B]return False[/B]
        [B]return True[/B]
    The "_" is to show indentation.
    However Python returns a "ValueError : too many values to unpack" error when I run it. I'm guessing it has to do with the line "for (base1, base2) in (strand1, strand2), but how to fix this error? Is there any way to show that I want two lists to be processed simultaneously, ie, character x of both strand1 and strand2?

    Thanks.
    Last edited by bvdet; Oct 27 '09, 04:41 PM. Reason: Add code tags
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    AuroraBorealis,

    Please use code tags when posting code.

    This is what happens when you iterate on (strand1, strand2):
    Code:
    >>> for item in (strand1, strand2):
    ... 	print item
    ... 	
    CCAGTCCACC
    GGTCAGGTGG
    >>>
    Iterate on one of the strings using builtin function enumerate(), and access the other string by index. I would also suggest using a dictionary to determine the complement.
    Code:
    seqDict = {'A':'T', 'T':'A', 'C':'G', 'G':'C'}
    
    def comp_DNA(s1, s2):
        for i, c in enumerate(s1):
            if s2[i] != seqDict[c]:
                return False
        return True
    
    print comp_DNA("CCAGTCCACC", "GGTCAGGTGG")
    print comp_DNA("CCAGTCCACC", "CGTCAGGTGG")

    Comment

    • AuroraBorealis
      New Member
      • Oct 2009
      • 4

      #3
      Hi thanks for reply.
      Enumerate function basically returns a string or list in the form of ordered pairs right? enumerate('ABC' ) would turn into (0, 'A'), (1, 'B'), (2, 'C')?
      Sorry I forgot to mention that I cannot use dictionary (I just started this course), as we haven't covered them yet, so I have no idea how dictionaries work. Is there some way to only do it with loops and enumerate if that's necessary?

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Sure. You could make use of the str method index(). Example:
        Code:
        s1 = "ATCG"
        s2 = "TAGC"
        
        for i, c in enumerate(strand1):
            if s1.index(c) != s2.index(strand2[i]):
                ................

        Comment

        • AuroraBorealis
          New Member
          • Oct 2009
          • 4

          #5
          Doesn't the index method return an integer index, if I provide it with a variable or a character, ie, basically find where in the string/list that character first appears?
          Could you explain what s1.index(c) would do please? And does s2.index(strand 2[i]) mean return the position of the ith character in strand2, as it appears in s2? Thanks.

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Your data strings are assigned to s1 and s2. str method index(substring ) returns the index number of the substring. Example:
            Code:
            >>> "ABCDEF".index("DEF")
            3
            >>>
            If the index number of one of the data strings for each character in strand1 is the same as the index number of the other data string for each corresponding character in strand2, then the characters are complmentary.
            Code:
            >>> strand1
            'CCAGTCCACC'
            >>> strand2
            'GGTCAGGAGC'
            >>> s1
            'ATCG'
            >>> s2
            'TAGC'
            >>> s1.index(strand1[0])
            2
            >>> s2.index(strand2[0])
            2
            >>> s1.index(strand1[-1])
            2
            >>> s2.index(strand2[-1])
            3
            >>>
            If a character does not occur in the data string, a ValueError is raised.
            Code:
            >>> "AA".index("B")
            Traceback (most recent call last):
              File "<interactive input>", line 1, in ?
            ValueError: substring not found
            >>>

            Comment

            Working...