How to compare two DNA sequences and return the identical nucleotides in a pair list










-4














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]]









share|improve this question



















  • 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















-4














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]]









share|improve this question



















  • 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













-4












-4








-4







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]]









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 '18 at 23:46









Jon Clements

98.4k19173219




98.4k19173219










asked Nov 11 '18 at 23:45









sir_Ouss

13




13







  • 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












  • 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







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












2 Answers
2






active

oldest

votes


















1














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.






share|improve this answer


















  • 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


















0














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]]







share|improve this answer






















  • 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










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
);



);













draft saved

draft discarded


















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









1














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.






share|improve this answer


















  • 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















1














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.






share|improve this answer


















  • 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













1












1








1






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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








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












  • 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













0














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]]







share|improve this answer






















  • 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















0














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]]







share|improve this answer






















  • 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













0












0








0






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]]







share|improve this answer














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]]








share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 '18 at 0:25

























answered Nov 12 '18 at 0:12









DMfll

99011830




99011830











  • 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















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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo