使用 python 进行 DNA 测序
DNA sequencing using python
使用循环,我如何在 python 中编写一个函数来对最长的蛋白质链进行排序,而不考虑顺序。函数 returns 仅由字符 'A'、'C'、'G' 和 'T' 组成的子字符串,当领带与其他元素混合时:示例,在序列中:'ACCGXXCXXGTTACTGGGCXTTGT',returns 'GTTACTGGGC'
如果数据以字符串形式提供,您可以简单地将其按字符 'X' 拆分,从而得到一个列表。
startstring = 'ACCGXXCXXGTTACTGGGCXTTGT'
array = startstring.split('X')
然后在检查元素长度的同时遍历列表会给你正确的结果:
# Initialize placeholders for comparison
temp_max_string = ''
temp_max_length = 0
#Loop over each string in the list
for i in array:
# Check if the current substring is longer than the longest found so far
if len(i) > temp_max_length:
# Replace the placeholders if it is longer
temp_max_length = len(i)
temp_max_string = i
print(temp_max_string) # or 'print temp_max_string' if you are using python2.
您还可以使用 python 内置函数以更有效的方式获得结果:
按长度降序排列(list.sort()
)
startstring = 'ACCGXXCXXGTTACTGGGCXTTGT'
array = startstring.split('X')
array.sort(key=len, reverse=True)
print(array[0]) #print the longest since we sorted for descending lengths
print(len(array[0])) # Would give you the length of the longest substring
只取最长的子串(max()
):
startstring = 'ACCGXXCXXGTTACTGGGCXTTGT'
array = startstring.split('X')
longest = max(array, key=len)
print(longest) # gives the longest substring
print(len(longest)) # gives you the length of the longest substring
使用循环,我如何在 python 中编写一个函数来对最长的蛋白质链进行排序,而不考虑顺序。函数 returns 仅由字符 'A'、'C'、'G' 和 'T' 组成的子字符串,当领带与其他元素混合时:示例,在序列中:'ACCGXXCXXGTTACTGGGCXTTGT',returns 'GTTACTGGGC'
如果数据以字符串形式提供,您可以简单地将其按字符 'X' 拆分,从而得到一个列表。
startstring = 'ACCGXXCXXGTTACTGGGCXTTGT'
array = startstring.split('X')
然后在检查元素长度的同时遍历列表会给你正确的结果:
# Initialize placeholders for comparison
temp_max_string = ''
temp_max_length = 0
#Loop over each string in the list
for i in array:
# Check if the current substring is longer than the longest found so far
if len(i) > temp_max_length:
# Replace the placeholders if it is longer
temp_max_length = len(i)
temp_max_string = i
print(temp_max_string) # or 'print temp_max_string' if you are using python2.
您还可以使用 python 内置函数以更有效的方式获得结果:
按长度降序排列(list.sort()
)
startstring = 'ACCGXXCXXGTTACTGGGCXTTGT'
array = startstring.split('X')
array.sort(key=len, reverse=True)
print(array[0]) #print the longest since we sorted for descending lengths
print(len(array[0])) # Would give you the length of the longest substring
只取最长的子串(max()
):
startstring = 'ACCGXXCXXGTTACTGGGCXTTGT'
array = startstring.split('X')
longest = max(array, key=len)
print(longest) # gives the longest substring
print(len(longest)) # gives you the length of the longest substring