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.
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): - >>> 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")
-
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 2,851
Expert Mod 2GB
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]):
-
................
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 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: - >>> "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
-
>>>
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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 ""
...
|
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...
|
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 =...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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
|
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...
| |