在多行上输出,我需要每行要分配给其自己的变量
下面的代码将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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该功能需要更灵活,才能允许任何数量的匹配项。
该函数不应负责结果。
因此,让我们返回列表并处理呼叫者中的演示文稿。例如: -
输出:
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:-
Output:
如果您知道函数返回多少值,则可以将每个函数分配给单独的变量。
分配的右侧必须是一个序列,可以将其恰好在左侧的表达式中拆开。
如果您无法预测该函数将返回多少值,那么无论如何,这是告诉您这是一个坏主意的众多方法之一。实际上,您可以使用单个变量中的所有值(或者,取决于您的用例,也许是字典或更复杂的数据结构,可能包含在类中)。
If you know how many values the function returns, you can assign each to a separate variable.
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。对于较大或未知数据,将它们封装在课堂上是一个更好的主意。
I guess this could help you with your project:
I agree with @tripleee. Encapsulating them in a class is a better idea for larger or unknown data.