-1

The code below takes dna_string2 and matches it within dna_string1. It then outputs the index location of the match or matches and then increments then return value by 1 to simulate "counting itself". The problem I am facing is that I need the output values of 2, 4, and 10 to be assigned to their own variables. I cannot figure out how to separate the output so that I can assign to individual variables. I have tried using split(). I have tried writing to a file first. I feel like I have tried just about everything to get the output separated. Any expert help would be greatly appreciated.

defined function

def get_most_likely_ancestor(dna_string1, dna_string2):
    i = 0
    g = len(dna_string2)
    for i in range (len(dna_string1)):
        i += 1
        g += 1
        if (dna_string1[i:g]) == (dna_string2):
            locations = dna_string1.index(dna_string2, i)
            locations += 1
            return locations

Function input

dna_string1 = "GATATATGCATATACTT"

dna_string2 = "ATAT"

function output (exactly as shown)

2

4

10

  • additionally, when I try to append the results to an empty list, I still get 3 lines of output like so line1 -> [2] | line2 -> [2,4] | line3 -> [2,4,10]. When I try to any of my outputs to a file, it only writes the last line. But when I read it back, it reads all the lines back instead of just the line that was written in. – STEAMxGaming May 10 '22 at 17:50
  • What is the problem? `a, b, c = lst` will assign `lst[0]` to `a`, `lst[1]` to `b`, and `lst[2]` to `c`, provided `lst` contains exactly three values. Assigning each to a separate variable is almost never the right thing to do, though. Why exactly is keeping the values in a list not acceptable, or even a far superior solution? – tripleee May 10 '22 at 17:59
  • Assigning them to variables is part of the requirement for the task I have. If they have to go to a list first then that is not a big deal. But at the end of the day I need variables assigned to the values of the indexes at which matches occur. – STEAMxGaming May 10 '22 at 18:03
  • Do you always know how many significant values will be returned? What if string2 doesn't match anywhere in string1. What if there are 100 matches? This assignment is fundamentally flawed. Just return a list with the relevant values and work with that. And what do you mean by "function output (exactly as shown)". Do you mean that the function should print those values or that it should return something that can be printed like that? More clarity needed – DarkKnight May 10 '22 at 18:25

3 Answers3

0

If you know how many values the function returns, you can assign each to a separate variable.

first, second, third = get_most_likely_ancestor(dna_string1, dna_string2)

The right-hand side of the assignment needs to be a sequence which can be unpacked into exactly the number of expressions on the left-hand side.

If you can't predict how many values the function will return, it's one of nature's many ways to tell you this is a bad idea anyway. You are in fact much better off with all the values in a single variable which is a list (or, depending on your use case, perhaps a dictionary, or a more complex data structure, probably encapsulated in a class).

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables is the canonical for "see, all the solutions which don't simply use a dictionary are horrible crocks". – tripleee May 10 '22 at 18:29
0

The function needs to be more flexible to allow for any number of matches.

The function should not be responsible for presentation of the result.

Therefore, let's just return a list and handle the presentation in the caller. For example:-

def get_most_likely_ancestor(s1, s2):
    offset = 0
    olist = []
    while (i := s1[offset:].find(s2)) >= 0:
        olist.append(offset := offset + i + 1)
    return olist

for pos in get_most_likely_ancestor('GATATATGCATATACTT', 'ATAT'):
    print(pos)

Output:

2
4
10
DarkKnight
  • 19,739
  • 3
  • 6
  • 22
-1

I guess this could help you with your project:

def get_most_likely_ancestor(dna_string1, dna_string2):
    i = 0
    g = len(dna_string2)
    global list_plain
    list_plain = []
    for i in range (len(dna_string1)):
        i += 1
        g += 1
        if (dna_string1[i:g]) == (dna_string2):
            locations = dna_string1.index(dna_string2, i)
            locations += 1
            list_plain.append(locations)
            
            


dna_string1 = "GATATATGCATATACTT"

dna_string2 = "ATAT"


get_most_likely_ancestor(dna_string1,dna_string2)
print(list_plain)

I agree with @tripleee. Encapsulating them in a class is a better idea for larger or unknown data.

  • This does not provide the required output. It is an excellent demonstration of how **not** to do this. – DarkKnight May 10 '22 at 18:55
  • It appends the output of function to a list and gives the same result. I agree your answer is better and more professional but still gives the same result. He wanted to assign each result to a variable – romancedawn May 10 '22 at 19:00
  • Also, it has the same output with your answer – romancedawn May 10 '22 at 19:09