How to compare two DNA sequences and return the identical nucleotides in a pair list
I want to compare two DNA sequences and return the identical nucleotides in a pair list (position in sequence 1, position in sequence 2)
input:
a = [G, T, T, U, I, P]
b = [E, G, T, P]
output:
[[0,1], [1,2], [2,2], [5,3]]
python
add a comment |
I want to compare two DNA sequences and return the identical nucleotides in a pair list (position in sequence 1, position in sequence 2)
input:
a = [G, T, T, U, I, P]
b = [E, G, T, P]
output:
[[0,1], [1,2], [2,2], [5,3]]
python
1
Are you after all pairs? So if you hada=['T', 'T', 'T']; b = ['T', 'T', 'T']
you'd have 9 results?
– Jon Clements♦
Nov 11 '18 at 23:49
5
Did you write any code for this? You need to share the code and explain what exact issue you are facing in that
– Chetan Ranpariya
Nov 11 '18 at 23:50
add a comment |
I want to compare two DNA sequences and return the identical nucleotides in a pair list (position in sequence 1, position in sequence 2)
input:
a = [G, T, T, U, I, P]
b = [E, G, T, P]
output:
[[0,1], [1,2], [2,2], [5,3]]
python
I want to compare two DNA sequences and return the identical nucleotides in a pair list (position in sequence 1, position in sequence 2)
input:
a = [G, T, T, U, I, P]
b = [E, G, T, P]
output:
[[0,1], [1,2], [2,2], [5,3]]
python
python
edited Nov 11 '18 at 23:46
Jon Clements♦
98.2k19173218
98.2k19173218
asked Nov 11 '18 at 23:45
sir_Ouss
13
13
1
Are you after all pairs? So if you hada=['T', 'T', 'T']; b = ['T', 'T', 'T']
you'd have 9 results?
– Jon Clements♦
Nov 11 '18 at 23:49
5
Did you write any code for this? You need to share the code and explain what exact issue you are facing in that
– Chetan Ranpariya
Nov 11 '18 at 23:50
add a comment |
1
Are you after all pairs? So if you hada=['T', 'T', 'T']; b = ['T', 'T', 'T']
you'd have 9 results?
– Jon Clements♦
Nov 11 '18 at 23:49
5
Did you write any code for this? You need to share the code and explain what exact issue you are facing in that
– Chetan Ranpariya
Nov 11 '18 at 23:50
1
1
Are you after all pairs? So if you had
a=['T', 'T', 'T']; b = ['T', 'T', 'T']
you'd have 9 results?– Jon Clements♦
Nov 11 '18 at 23:49
Are you after all pairs? So if you had
a=['T', 'T', 'T']; b = ['T', 'T', 'T']
you'd have 9 results?– Jon Clements♦
Nov 11 '18 at 23:49
5
5
Did you write any code for this? You need to share the code and explain what exact issue you are facing in that
– Chetan Ranpariya
Nov 11 '18 at 23:50
Did you write any code for this? You need to share the code and explain what exact issue you are facing in that
– Chetan Ranpariya
Nov 11 '18 at 23:50
add a comment |
2 Answers
2
active
oldest
votes
You can do it with for loops:
a_s = ["G", "T", "T", "U", "I", "P"]
b_s = ["E", "G", "T", "P"]
d =
for i,a in enumerate(a_s):
for j,b in enumerate(b_s):
if a == b:
d.append([i,j])
print(d)
Out:
[[0, 1], [1, 2], [2, 2], [5, 3]]
Or you can do it in a single row:
a_s = ["G", "T", "T", "U", "I", "P"]
b_s = ["E", "G", "T", "P"]
print([[x, y] for x, av in enumerate(a_s) for y, bv in enumerate(b_s) if av == bv])
With the above, same output.
Note:
The first version is in most case more readable, the second is more concise. You can always chose any of both depending on the code context and the purpose of it.
2
Or rolled into a list-comp:[[x, y] for x, av in enumerate(a) for y, bv in enumerate(b) if av == bv]
– Jon Clements♦
Nov 11 '18 at 23:52
@JonClements Sometimes more readable the open format, but can show him as well
– Geeocode
Nov 11 '18 at 23:54
Indeed... if you're starting out it's definitely more readable. However, it's useful for learning and for others in the future that come across this post to see the other version - doesn't hurt to show both
– Jon Clements♦
Nov 11 '18 at 23:57
thank you so much , that's exactly what i want
– sir_Ouss
Nov 12 '18 at 1:38
@sir_Ouss You'r welcome!
– Geeocode
Nov 12 '18 at 1:46
|
show 3 more comments
Two examples leveraging "product" from the "itertools" module.
The first is a traditional for loop that appends a list.
The second is a list comprehension equivalent.
from itertools import product
a = list('GTTUIP')
b = list('EGTP')
# Without a comprehension.
results =
for (x, a_s), (y, b_s) in product(enumerate(a), enumerate(b)):
if a_s == b_s:
results.append([x, y])
print(results)
# With a comprehension
results = [[x, y]
for (x, a_s), (y, b_s)
in product(enumerate(a), enumerate(b))
if a_s == b_s]
print(results)
OUT:
[[0, 1], [1, 2], [2, 2], [5, 3]]
[[0, 1], [1, 2], [2, 2], [5, 3]]
For what it's worth, usingitertools.product
is roughly 10% faster. See this gist using thetimeit
module. Each comprehension is run 1 million times: gist.github.com/bb4f02e391c7e15df947df6918e7bd93
– DMfll
Nov 12 '18 at 10:56
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254390%2fhow-to-compare-two-dna-sequences-and-return-the-identical-nucleotides-in-a-pair%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can do it with for loops:
a_s = ["G", "T", "T", "U", "I", "P"]
b_s = ["E", "G", "T", "P"]
d =
for i,a in enumerate(a_s):
for j,b in enumerate(b_s):
if a == b:
d.append([i,j])
print(d)
Out:
[[0, 1], [1, 2], [2, 2], [5, 3]]
Or you can do it in a single row:
a_s = ["G", "T", "T", "U", "I", "P"]
b_s = ["E", "G", "T", "P"]
print([[x, y] for x, av in enumerate(a_s) for y, bv in enumerate(b_s) if av == bv])
With the above, same output.
Note:
The first version is in most case more readable, the second is more concise. You can always chose any of both depending on the code context and the purpose of it.
2
Or rolled into a list-comp:[[x, y] for x, av in enumerate(a) for y, bv in enumerate(b) if av == bv]
– Jon Clements♦
Nov 11 '18 at 23:52
@JonClements Sometimes more readable the open format, but can show him as well
– Geeocode
Nov 11 '18 at 23:54
Indeed... if you're starting out it's definitely more readable. However, it's useful for learning and for others in the future that come across this post to see the other version - doesn't hurt to show both
– Jon Clements♦
Nov 11 '18 at 23:57
thank you so much , that's exactly what i want
– sir_Ouss
Nov 12 '18 at 1:38
@sir_Ouss You'r welcome!
– Geeocode
Nov 12 '18 at 1:46
|
show 3 more comments
You can do it with for loops:
a_s = ["G", "T", "T", "U", "I", "P"]
b_s = ["E", "G", "T", "P"]
d =
for i,a in enumerate(a_s):
for j,b in enumerate(b_s):
if a == b:
d.append([i,j])
print(d)
Out:
[[0, 1], [1, 2], [2, 2], [5, 3]]
Or you can do it in a single row:
a_s = ["G", "T", "T", "U", "I", "P"]
b_s = ["E", "G", "T", "P"]
print([[x, y] for x, av in enumerate(a_s) for y, bv in enumerate(b_s) if av == bv])
With the above, same output.
Note:
The first version is in most case more readable, the second is more concise. You can always chose any of both depending on the code context and the purpose of it.
2
Or rolled into a list-comp:[[x, y] for x, av in enumerate(a) for y, bv in enumerate(b) if av == bv]
– Jon Clements♦
Nov 11 '18 at 23:52
@JonClements Sometimes more readable the open format, but can show him as well
– Geeocode
Nov 11 '18 at 23:54
Indeed... if you're starting out it's definitely more readable. However, it's useful for learning and for others in the future that come across this post to see the other version - doesn't hurt to show both
– Jon Clements♦
Nov 11 '18 at 23:57
thank you so much , that's exactly what i want
– sir_Ouss
Nov 12 '18 at 1:38
@sir_Ouss You'r welcome!
– Geeocode
Nov 12 '18 at 1:46
|
show 3 more comments
You can do it with for loops:
a_s = ["G", "T", "T", "U", "I", "P"]
b_s = ["E", "G", "T", "P"]
d =
for i,a in enumerate(a_s):
for j,b in enumerate(b_s):
if a == b:
d.append([i,j])
print(d)
Out:
[[0, 1], [1, 2], [2, 2], [5, 3]]
Or you can do it in a single row:
a_s = ["G", "T", "T", "U", "I", "P"]
b_s = ["E", "G", "T", "P"]
print([[x, y] for x, av in enumerate(a_s) for y, bv in enumerate(b_s) if av == bv])
With the above, same output.
Note:
The first version is in most case more readable, the second is more concise. You can always chose any of both depending on the code context and the purpose of it.
You can do it with for loops:
a_s = ["G", "T", "T", "U", "I", "P"]
b_s = ["E", "G", "T", "P"]
d =
for i,a in enumerate(a_s):
for j,b in enumerate(b_s):
if a == b:
d.append([i,j])
print(d)
Out:
[[0, 1], [1, 2], [2, 2], [5, 3]]
Or you can do it in a single row:
a_s = ["G", "T", "T", "U", "I", "P"]
b_s = ["E", "G", "T", "P"]
print([[x, y] for x, av in enumerate(a_s) for y, bv in enumerate(b_s) if av == bv])
With the above, same output.
Note:
The first version is in most case more readable, the second is more concise. You can always chose any of both depending on the code context and the purpose of it.
edited Nov 11 '18 at 23:58
answered Nov 11 '18 at 23:52
Geeocode
2,2261820
2,2261820
2
Or rolled into a list-comp:[[x, y] for x, av in enumerate(a) for y, bv in enumerate(b) if av == bv]
– Jon Clements♦
Nov 11 '18 at 23:52
@JonClements Sometimes more readable the open format, but can show him as well
– Geeocode
Nov 11 '18 at 23:54
Indeed... if you're starting out it's definitely more readable. However, it's useful for learning and for others in the future that come across this post to see the other version - doesn't hurt to show both
– Jon Clements♦
Nov 11 '18 at 23:57
thank you so much , that's exactly what i want
– sir_Ouss
Nov 12 '18 at 1:38
@sir_Ouss You'r welcome!
– Geeocode
Nov 12 '18 at 1:46
|
show 3 more comments
2
Or rolled into a list-comp:[[x, y] for x, av in enumerate(a) for y, bv in enumerate(b) if av == bv]
– Jon Clements♦
Nov 11 '18 at 23:52
@JonClements Sometimes more readable the open format, but can show him as well
– Geeocode
Nov 11 '18 at 23:54
Indeed... if you're starting out it's definitely more readable. However, it's useful for learning and for others in the future that come across this post to see the other version - doesn't hurt to show both
– Jon Clements♦
Nov 11 '18 at 23:57
thank you so much , that's exactly what i want
– sir_Ouss
Nov 12 '18 at 1:38
@sir_Ouss You'r welcome!
– Geeocode
Nov 12 '18 at 1:46
2
2
Or rolled into a list-comp:
[[x, y] for x, av in enumerate(a) for y, bv in enumerate(b) if av == bv]
– Jon Clements♦
Nov 11 '18 at 23:52
Or rolled into a list-comp:
[[x, y] for x, av in enumerate(a) for y, bv in enumerate(b) if av == bv]
– Jon Clements♦
Nov 11 '18 at 23:52
@JonClements Sometimes more readable the open format, but can show him as well
– Geeocode
Nov 11 '18 at 23:54
@JonClements Sometimes more readable the open format, but can show him as well
– Geeocode
Nov 11 '18 at 23:54
Indeed... if you're starting out it's definitely more readable. However, it's useful for learning and for others in the future that come across this post to see the other version - doesn't hurt to show both
– Jon Clements♦
Nov 11 '18 at 23:57
Indeed... if you're starting out it's definitely more readable. However, it's useful for learning and for others in the future that come across this post to see the other version - doesn't hurt to show both
– Jon Clements♦
Nov 11 '18 at 23:57
thank you so much , that's exactly what i want
– sir_Ouss
Nov 12 '18 at 1:38
thank you so much , that's exactly what i want
– sir_Ouss
Nov 12 '18 at 1:38
@sir_Ouss You'r welcome!
– Geeocode
Nov 12 '18 at 1:46
@sir_Ouss You'r welcome!
– Geeocode
Nov 12 '18 at 1:46
|
show 3 more comments
Two examples leveraging "product" from the "itertools" module.
The first is a traditional for loop that appends a list.
The second is a list comprehension equivalent.
from itertools import product
a = list('GTTUIP')
b = list('EGTP')
# Without a comprehension.
results =
for (x, a_s), (y, b_s) in product(enumerate(a), enumerate(b)):
if a_s == b_s:
results.append([x, y])
print(results)
# With a comprehension
results = [[x, y]
for (x, a_s), (y, b_s)
in product(enumerate(a), enumerate(b))
if a_s == b_s]
print(results)
OUT:
[[0, 1], [1, 2], [2, 2], [5, 3]]
[[0, 1], [1, 2], [2, 2], [5, 3]]
For what it's worth, usingitertools.product
is roughly 10% faster. See this gist using thetimeit
module. Each comprehension is run 1 million times: gist.github.com/bb4f02e391c7e15df947df6918e7bd93
– DMfll
Nov 12 '18 at 10:56
add a comment |
Two examples leveraging "product" from the "itertools" module.
The first is a traditional for loop that appends a list.
The second is a list comprehension equivalent.
from itertools import product
a = list('GTTUIP')
b = list('EGTP')
# Without a comprehension.
results =
for (x, a_s), (y, b_s) in product(enumerate(a), enumerate(b)):
if a_s == b_s:
results.append([x, y])
print(results)
# With a comprehension
results = [[x, y]
for (x, a_s), (y, b_s)
in product(enumerate(a), enumerate(b))
if a_s == b_s]
print(results)
OUT:
[[0, 1], [1, 2], [2, 2], [5, 3]]
[[0, 1], [1, 2], [2, 2], [5, 3]]
For what it's worth, usingitertools.product
is roughly 10% faster. See this gist using thetimeit
module. Each comprehension is run 1 million times: gist.github.com/bb4f02e391c7e15df947df6918e7bd93
– DMfll
Nov 12 '18 at 10:56
add a comment |
Two examples leveraging "product" from the "itertools" module.
The first is a traditional for loop that appends a list.
The second is a list comprehension equivalent.
from itertools import product
a = list('GTTUIP')
b = list('EGTP')
# Without a comprehension.
results =
for (x, a_s), (y, b_s) in product(enumerate(a), enumerate(b)):
if a_s == b_s:
results.append([x, y])
print(results)
# With a comprehension
results = [[x, y]
for (x, a_s), (y, b_s)
in product(enumerate(a), enumerate(b))
if a_s == b_s]
print(results)
OUT:
[[0, 1], [1, 2], [2, 2], [5, 3]]
[[0, 1], [1, 2], [2, 2], [5, 3]]
Two examples leveraging "product" from the "itertools" module.
The first is a traditional for loop that appends a list.
The second is a list comprehension equivalent.
from itertools import product
a = list('GTTUIP')
b = list('EGTP')
# Without a comprehension.
results =
for (x, a_s), (y, b_s) in product(enumerate(a), enumerate(b)):
if a_s == b_s:
results.append([x, y])
print(results)
# With a comprehension
results = [[x, y]
for (x, a_s), (y, b_s)
in product(enumerate(a), enumerate(b))
if a_s == b_s]
print(results)
OUT:
[[0, 1], [1, 2], [2, 2], [5, 3]]
[[0, 1], [1, 2], [2, 2], [5, 3]]
edited Nov 12 '18 at 0:25
answered Nov 12 '18 at 0:12
DMfll
99011830
99011830
For what it's worth, usingitertools.product
is roughly 10% faster. See this gist using thetimeit
module. Each comprehension is run 1 million times: gist.github.com/bb4f02e391c7e15df947df6918e7bd93
– DMfll
Nov 12 '18 at 10:56
add a comment |
For what it's worth, usingitertools.product
is roughly 10% faster. See this gist using thetimeit
module. Each comprehension is run 1 million times: gist.github.com/bb4f02e391c7e15df947df6918e7bd93
– DMfll
Nov 12 '18 at 10:56
For what it's worth, using
itertools.product
is roughly 10% faster. See this gist using the timeit
module. Each comprehension is run 1 million times: gist.github.com/bb4f02e391c7e15df947df6918e7bd93– DMfll
Nov 12 '18 at 10:56
For what it's worth, using
itertools.product
is roughly 10% faster. See this gist using the timeit
module. Each comprehension is run 1 million times: gist.github.com/bb4f02e391c7e15df947df6918e7bd93– DMfll
Nov 12 '18 at 10:56
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254390%2fhow-to-compare-two-dna-sequences-and-return-the-identical-nucleotides-in-a-pair%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Are you after all pairs? So if you had
a=['T', 'T', 'T']; b = ['T', 'T', 'T']
you'd have 9 results?– Jon Clements♦
Nov 11 '18 at 23:49
5
Did you write any code for this? You need to share the code and explain what exact issue you are facing in that
– Chetan Ranpariya
Nov 11 '18 at 23:50