在多行上输出,我需要每行要分配给其自己的变量

发布于 2025-01-27 18:34:47 字数 790 浏览 1 评论 0原文

下面的代码将DNA_STRING2匹配并在DNA_STRING1中匹配。然后,它输出匹配或匹配的索引位置,然后递增,然后以1返回值,以模拟“计数自身”。我面临的问题是,我需要将2、4和10的输出值分配给其自己的变量。我无法弄清楚如何分离输出,以便可以分配给单个变量。我尝试使用split()。我尝试先写入文件。我觉得我已经尝试了几乎所有的东西来分开输出。任何专家的帮助将不胜感激。

定义的函数

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

函数输入

dna_string1 =“ gatatatgcatatactt”

dna_string2 =“ atat”

功能功能输出 (完全如图所示)

2

4

10

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绅士风度i 2025-02-03 18:34:47

该功能需要更灵活,才能允许任何数量的匹配项。

该函数不应负责结果。

因此,让我们返回列表并处理呼叫者中的演示文稿。例如: -

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)

输出:

2
4
10

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
怀里藏娇 2025-02-03 18:34:47

如果您知道函数返回多少值,则可以将每个函数分配给单独的变量。

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

分配的右侧必须是一个序列,可以将其恰好在左侧的表达式中拆开。

如果您无法预测该函数将返回多少值,那么无论如何,这是告诉您这是一个坏主意的众多方法之一。实际上,您可以使用单个变量中的所有值(或者,取决于您的用例,也许是字典或更复杂的数据结构,可能包含在类中)。

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

成熟稳重的好男人 2025-02-03 18:34:47

我想这可以帮助您完成您的项目:

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)

我同意@tripleee。对于较大或未知数据,将它们封装在课堂上是一个更好的主意。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文