Connecting Tech Pros Worldwide Forums | Help | Site Map

"Value Error" in Python

Newbie
 
Join Date: Oct 2009
Posts: 3
#1: 4 Weeks Ago
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:
Expand|Select|Wrap|Line Numbers
  1. for (base1, base2) in (strand1, strand2):
  2.     if (base1, base2) not in (('A', 'T'), ('T', 'A'), ('C', 'G'), ('G', 'C')):
  3.         return False
  4.     return True
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.

bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,567
#2: 4 Weeks Ago

re: "Value Error" in Python


AuroraBorealis,

Please use code tags when posting code.

This is what happens when you iterate on (strand1, strand2):
Expand|Select|Wrap|Line Numbers
  1. >>> for item in (strand1, strand2):
  2. ...     print item
  3. ...     
  4. CCAGTCCACC
  5. GGTCAGGTGG
  6. >>> 
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.
Expand|Select|Wrap|Line Numbers
  1. seqDict = {'A':'T', 'T':'A', 'C':'G', 'G':'C'}
  2.  
  3. def comp_DNA(s1, s2):
  4.     for i, c in enumerate(s1):
  5.         if s2[i] != seqDict[c]:
  6.             return False
  7.     return True
  8.  
  9. print comp_DNA("CCAGTCCACC", "GGTCAGGTGG")
  10. print comp_DNA("CCAGTCCACC", "CGTCAGGTGG")
  11.  
Newbie
 
Join Date: Oct 2009
Posts: 3
#3: 4 Weeks Ago

re: "Value Error" in Python


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?
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,567
#4: 4 Weeks Ago

re: "Value Error" in Python


Sure. You could make use of the str method index(). Example:
Expand|Select|Wrap|Line Numbers
  1. s1 = "ATCG"
  2. s2 = "TAGC"
  3.  
  4. for i, c in enumerate(strand1):
  5.     if s1.index(c) != s2.index(strand2[i]):
  6.         ................
Newbie
 
Join Date: Oct 2009
Posts: 3
#5: 4 Weeks Ago

re: "Value Error" in Python


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(strand2[i]) mean return the position of the ith character in strand2, as it appears in s2? Thanks.
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,567
#6: 4 Weeks Ago

re: "Value Error" in Python


Your data strings are assigned to s1 and s2. str method index(substring) returns the index number of the substring. Example:
Expand|Select|Wrap|Line Numbers
  1. >>> "ABCDEF".index("DEF")
  2. 3
  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.
Expand|Select|Wrap|Line Numbers
  1. >>> strand1
  2. 'CCAGTCCACC'
  3. >>> strand2
  4. 'GGTCAGGAGC'
  5. >>> s1
  6. 'ATCG'
  7. >>> s2
  8. 'TAGC'
  9. >>> s1.index(strand1[0])
  10. 2
  11. >>> s2.index(strand2[0])
  12. 2
  13. >>> s1.index(strand1[-1])
  14. 2
  15. >>> s2.index(strand2[-1])
  16. 3
  17. >>> 
If a character does not occur in the data string, a ValueError is raised.
Expand|Select|Wrap|Line Numbers
  1. >>> "AA".index("B")
  2. Traceback (most recent call last):
  3.   File "<interactive input>", line 1, in ?
  4. ValueError: substring not found
  5. >>> 
Reply