调整Fucntion以找到一个以上的位置

发布于 2025-02-14 01:43:26 字数 404 浏览 6 评论 0原文

我创建了此功能,并在DNA序列中找到了基础的位置。像dna = ['a','g','c','g','t','a','g','t','t','c','g',a','t ','c','a','a','t','t','a','t','a','c','g',g',a','','t','t',t',t',' 'c','g','g','g', 't','a','t']。我需要它在一次“ A''T”之类的时间找到一个以上的基础。谁能帮忙?

def position(list, value):
     pos = []
     for n in range(len(list)):
             if list[n] == value:
                     pos.append(n)
     return pos

I created this function and it finds the location of the base in a dna sequences. Like dna = ['A', 'G', 'C', 'G', 'T', 'A', 'G', 'T', 'C', 'G', 'A', 'T', 'C', 'A', 'A', 'T', 'T', 'A', 'T', 'A', 'C', 'G', 'A', 'T', 'C', 'G', 'G', 'G', 'T', 'A', 'T']. I need it to find more than one base at a time like 'A''T'. Can anyone help?

def position(list, value):
     pos = []
     for n in range(len(list)):
             if list[n] == value:
                     pos.append(n)
     return pos

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

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

发布评论

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

评论(4

七分※倦醒 2025-02-21 01:43:26

您可以将DNA序列用作字符串,然后使用正则序列:

import re

dna_str = ''.join(dna)

pattern = r'AT'

pos = [(i.start(0), i.end(0)) for i in re.finditer(pattern, dna_str)]
print(pos)

[(10, 12), (14, 16), (17, 19), (22, 24), (29, 31)]

You can work with the dna sequence as a string, and then use regex:

import re

dna_str = ''.join(dna)

pattern = r'AT'

pos = [(i.start(0), i.end(0)) for i in re.finditer(pattern, dna_str)]
print(pos)

[(10, 12), (14, 16), (17, 19), (22, 24), (29, 31)]
二手情话 2025-02-21 01:43:26

旁注,很好,不要将关键字用于可变名称。列表是Python关键字

def position(l: list, values: list): -> list
     pos = []
     for i, val in enumerate(l):
             if val in values:
                     pos.append(i)
     return pos

side note, good not to use keywords for variable names. list is a python keyword

def position(l: list, values: list): -> list
     pos = []
     for i, val in enumerate(l):
             if val in values:
                     pos.append(i)
     return pos
雨夜星沙 2025-02-21 01:43:26

您绝对应该使用Python内置功能。例如,

[n for n,x in enumerate(dna) if x == 'A']

如果您考虑一对字母对,则可以使用理解查找bigram而不是位置(list,value)可以减少到上述:

[n for n,x in enumerate(zip(dna[:-1], dna[1:])) if x==('A','T')]

如果您想找到“任一A的位置” '或't',您可以将其指定为条件

[n for n,x in enumerate(dna) if x in ('A', 'T')]

You should definitely use Python built-in functions. For instance, instead of position(list, value) you could use comprehension

[n for n,x in enumerate(dna) if x == 'A']

Finding a bigram could be reduced to the above if you consider pairs of letters:

[n for n,x in enumerate(zip(dna[:-1], dna[1:])) if x==('A','T')]

If instead you want to find the positions of either 'A' or 'T', you could just specify that as the condition

[n for n,x in enumerate(dna) if x in ('A', 'T')]
甜警司 2025-02-21 01:43:26

Python将有效地找到从任何点开始的字符串的子字符串。

def positions(dnalist, substr):
    dna = "".join(dnalist) # make single string
    st = 0
    pos = []
    while True: 
        a_pos = dna.find(substr, st)
        if a_pos < 0:
            return pos
        pos.append(a_pos)
        st = a_pos + 1

测试用法:

>>> testdna = ['A', 'G', 'C', 'G', 'T', 'A', 'G', 'T', 'C', 'G', 'A', 'T', 'C', 'A', 'A', 'T', 'T', 'A', 'T', 'A', 'C', 'G', 'A', 'T', 'C', 'G', 'G', 'G', 'T', 'A', 'T']
>>> positions(testdna, "AT")
[10, 14, 17, 22, 29]

Python will efficiently find a substring of a string starting from any point.

def positions(dnalist, substr):
    dna = "".join(dnalist) # make single string
    st = 0
    pos = []
    while True: 
        a_pos = dna.find(substr, st)
        if a_pos < 0:
            return pos
        pos.append(a_pos)
        st = a_pos + 1

Test usage:

>>> testdna = ['A', 'G', 'C', 'G', 'T', 'A', 'G', 'T', 'C', 'G', 'A', 'T', 'C', 'A', 'A', 'T', 'T', 'A', 'T', 'A', 'C', 'G', 'A', 'T', 'C', 'G', 'G', 'G', 'T', 'A', 'T']
>>> positions(testdna, "AT")
[10, 14, 17, 22, 29]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文