473,320 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

"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:
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.
Oct 27 '09 #1
5 7459
bvdet
2,851 Expert Mod 2GB
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.  
Oct 27 '09 #2
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?
Oct 27 '09 #3
bvdet
2,851 Expert Mod 2GB
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.         ................
Oct 27 '09 #4
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.
Oct 27 '09 #5
bvdet
2,851 Expert Mod 2GB
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. >>> 
Oct 27 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Follower | last post by:
Hi, I've run into an issue which seems to have been discussed previously on `python-dev` but only in context of Zope3: "Fun with 2.3 shutdown" -- Tim Peters...
4
by: Grzegorz Dostatni | last post by:
Good morning. I've got a Tkinter problem: File "/usr/lib/python2.3/lib-tk/Tkinter.py", line 2310, in selection_present return self.tk.getboolean( TclError: expected boolean value but got "" ...
2
by: Stephen Briley | last post by:
For some reason, my posts are scrubbed as attachments. Lets hope that sending from the yahoo account works. I'm new to Python and I'm trying to do some database work with MS Access, but I can't...
7
by: Petr Prikryl | last post by:
Hi, Summary: In my opinion, the C-like prefix increment and decrement operators (++i and --i) should be marked as "syntax error". Current situation: try... (Python 2.4 (#60, ...)) >>> i =...
1
by: qwejohn | last post by:
Hello, I had posted this question in the twisted mailing list but did not got a solution ; I hope that the python Gurus of this forum can help me a bit. I am trying the exmaple in the python...
6
by: bhochstetler | last post by:
I am on a hp 11.11 machine doing a 64 bit python 2.5 build. When I get my python executable created and run it, I get the error: "import site failed" OverflowError: signed integer is greater...
10
by: hall.jeff | last post by:
As a relative new comer to Python, I haven't done a heck of a lot of hacking around with it. I had my first run in with Python's quirky (to me at least) tendency to assign by reference rather than...
1
by: bruce | last post by:
i'm getting the following error: mechanize._response.httperror_seek_wrapper: HTTP Error 500: i'm running python 5.1 and mechanize 0.1.7b I have no idea as to what I have to...
0
by: Miles | last post by:
On Mon, Sep 15, 2008 at 6:06 AM, Harish K Vishwanath <harish.shastry@gmail.comwrote: "built-in type" generally means "implemented in C", also sometimes called "extension type". Both the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.