简体   繁体   English

如何在字符串中找到模式最后一次出现的位置,并使用这些位置从另一个字符串中提取子字符串

[英]How to find positions of the last occurrence of a pattern in a string, and use these to extract a substring from another string

I need some help with a specific problem, which I cannot seem to find on this website.我需要一些特定问题的帮助,我似乎在本网站上找不到。 I have a result which looks something like this:我有一个看起来像这样的结果:

result = "ooooooooooooooooooooooMMMMMMooooooooooooooooooMMMMMMooooooooooMMMMMMMMoo"

This is a transmembrane prediction.这是跨膜预测。 So for this string, I have another string of the same length, but is an amino acid code, for example:所以对于这个字符串,我有另一个相同长度的字符串,但是是一个氨基酸代码,例如:

amino_acid_code = "MSDENKSTPIVKASDITDKLKEDILTISKDALDKNTWHVIVGKNFGSYVTHEKGHFVYFYIGPLAFLVFKTA"

I want to do some research on the last "M" region.我想对最后一个“M”区做一些研究。 This can vary in length, as well as the "o" that comes after.这可以在长度上有所不同,以及后面的“o”。 So in this case I need to extract "PLAFLVFK" from the last string, which corresponds to the last "M" region.所以在这种情况下,我需要从最后一个字符串中提取“PLAFLVFK”,它对应于最后一个“M”区域。

I have something like this already, but I cannot figure out how to obtain the start position, and I also believe a simpler (or computationally better) solution is possible.我已经有了这样的东西,但我无法弄清楚如何获得开始位置,而且我也相信一个更简单(或计算上更好)的解决方案是可能的。

end = result.rfind('M')
start = ?
region_I_need = amino_acid_code[start:end]

Thanks in advance提前致谢

To also find the start position, use rfind again after slicing off the characters after the end of the result string:要同时找到起始位置, rfind在切掉result字符串末尾的字符后再次使用rfind

result = "ooooooooooooooooooooooMMMMMMooooooooooooooooooMMMMMMooooooooooMMMMMMMMoo"
amino_acid_code = "MSDENKSTPIVKASDITDKLKEDILTISKDALDKNTWHVIVGKNFGSYVTHEKGHFVYFYIGPLAFLVFKTA"

# add 1 to the indices to get the correct positions
end = result.rfind('M') + 1
start = result[:end].rfind('o') + 1
region_I_need = amino_acid_code[start:end]

print(start, end)
print(amino_acid_code[start:end])
>>> 62 70
>>> PLAFLVFK

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何找到给定字符串中最后一次出现的子字符串? - How to find last occurrence of a substring in a given string? 查找字符串中子字符串最后一次出现的索引 - Find index of last occurrence of a substring in a string 如何在 Swift 字符串中找到最后一次出现的子字符串? - How do I find the last occurrence of a substring in a Swift string? 如何从python中的字符串中提取标记和另一个标记的第n个出现之间的子字符串 - How to extract a substring from a string in python between a marker and the nth occurrence of another marker 如何从一段字符串中删除最后一次出现的 substring? - How to remove last occurrence of a substring from a picece of string? 在字符串对象中查找模式,然后提取子字符串 - Find a pattern in a string object then extract substring 如何从另一个字符串中删除最后一个字符串? - How to do remove the last occurrence of a string from another string? 如何从向量中提取特定“字符串”的最后一次出现 - How to extract last occurrence of a particular 'string' from a vector 查找字符串中第 n 次出现的 substring - Find the nth occurrence of substring in a string 使用模式从字符串中提取子字符串 - Extract substring from a string using pattern
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM