"Value Error" in Python | Newbie | | Join Date: Oct 2009
Posts: 3
| |
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: - for (base1, base2) in (strand1, strand2):
-
if (base1, base2) not in (('A', 'T'), ('T', 'A'), ('C', 'G'), ('G', 'C')):
-
return False
-
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.
|  | Moderator | | Join Date: Oct 2006 Location: Nashville, TN
Posts: 1,567
| | | re: "Value Error" in Python
AuroraBorealis,
Please use code tags when posting code.
This is what happens when you iterate on (strand1, strand2): - >>> 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. - 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")
-
| | Newbie | | Join Date: Oct 2009
Posts: 3
| | | 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?
|  | Moderator | | Join Date: Oct 2006 Location: Nashville, TN
Posts: 1,567
| | | re: "Value Error" in Python
Sure. You could make use of the str method index(). Example: - s1 = "ATCG"
-
s2 = "TAGC"
-
-
for i, c in enumerate(strand1):
-
if s1.index(c) != s2.index(strand2[i]):
-
................
| | Newbie | | Join Date: Oct 2009
Posts: 3
| | | 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.
|  | Moderator | | Join Date: Oct 2006 Location: Nashville, TN
Posts: 1,567
| | | 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: - >>> "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. - >>> 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. - >>> "AA".index("B")
-
Traceback (most recent call last):
-
File "<interactive input>", line 1, in ?
-
ValueError: substring not found
-
>>>
|  | | | | Forums
Visit our community forums for general discussions and latest on Bytes
/bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,567 network members.
|