尝试使用python以10的连续间隔查找字符串的特定字符

2024-04-20 02:27:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个由DNA组成的数据,存储为字母“a”、“g”、“c”和“t”的序列。在我的数据中,我使用了:

count = data.count('t')
print(count)

在Python中,找到“t”出现1514710次。你知道吗

我想找出“t”在10个字母的连续窗口中出现的次数,如下所示:example

从图中我可以看出,在第一组10个字母中,“t”出现3次(即在第一组10个字母中,“t”出现3次,共10个字母),“t”在第二组10个字母中出现2次。你知道吗

我希望在python脚本中显示每个窗口的编号,而不在每个编号后面加换行符。你知道吗

例如,如果我的数据如下所示:

    atgcttgcatgcttgcaaatgcatgcttgcattgcaa

我非常希望Python输出显示如下内容:

    't' appears twice in the first set of 10 letters, 
    and appears 4 times in the second set of 10 letters,
    and so on....

这就是我目前所尝试的:

    window_size = 10                                                                          
    windows_length = len(data) // window_size                                                
    windows = [data[i:i+windows_length] for i in range(0, len(data),                       windows_length)]        
    result = sum(1 if 't' in (x) else 0 for x in windows)  

显示11个

但我不确定这是不是正确的方法。 任何帮助都将不胜感激。非常感谢。你知道吗


Tags: andofthe数据indatawindowscount
3条回答

如果我理解正确,你想数一数有多少个窗口包含't'。然后我的方法是将data分解成windows,并计算其中有多少包含't'。你知道吗

window_size = 10                                                                          
windows_length = len(data) // window_size                                                
windows = [data[i:i+windows_length] for i in range(0, len(data), windows_length)]        
result = sum(1 if 't' in (x) else 0 for x in windows)    

您可以使用列表理解功能将数据分解为“窗口”列表:

windows: List[List[str]] = [data[i * 10:(i + 1) * 10] 
                            for i in range((len(data) + 10 - 1) // 10 )]

然后用同样的方法计算每个窗口的数量:

counts: List[int] = [window.count('t') 
                     for window in windows]

您没有指定打印输出的确切方式,所以我将剩下的留给您来确定,但请尝试print(counts)查看该格式是否适合您。你知道吗

如果dna序列是一个字符串,那么textwrap.wrap文件它返回包装行的列表(尽管可能有内存方面的考虑)。所以你可以写:

>>> from textwrap import wrap
>>> dna = 'atgcttgcatgcttgcaaatgcatgcttgcattgcaa'
>>> [chunk.count('t') for chunk in wrap(dna, 10)]
[4, 3, 3, 2]                                         

要获取块编号,可以使用枚举:

>>> print(*(f'On row #{i} "t" occured {chunk.count("t")} times' for i, chunk in enumerate(wrap(dna, 10), start=1)), sep='\n')
On row #1 "t" occured 4 times
On row #2 "t" occured 3 times
On row #3 "t" occured 3 times
On row #4 "t" occured 2 times

相关问题 更多 >