社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

Chris_Rands

Chris_Rands 最近创建的主题
Chris_Rands 最近回复了
3 年前
回复了 Chris_Rands 创建的主题 » 使用第三个无关项进行Python双列表压缩

使用无限迭代器:

from itertools import count
index = count()
self.Item = [[Object(x, y, next(index)) for y in range(3)] for x in range(3)]
7 年前
回复了 Chris_Rands 创建的主题 » For循环范围步骤更改为float python[duplicate]

令人惊讶的是,还没有人提到推荐的解决方案 in the Python 3 docs :

另见:

  • 这个 linspace recipe 演示如何实现适合浮点应用程序的范围的延迟版本。

一旦定义,配方很容易使用,不需要 numpy 或任何其他外部库,但函数类似于 numpy.linspace() . 请注意,而不是 step 争论,第三个 num 参数指定所需值的数目,例如:

print(linspace(0, 10, 5))
# linspace(0, 10, 5)
print(list(linspace(0, 10, 5)))
# [0.0, 2.5, 5.0, 7.5, 10]

我引用安德鲁·巴内特(Andrew Barnert)对Python 3完整配方的修改版本如下:

import collections.abc
import numbers

class linspace(collections.abc.Sequence):
    """linspace(start, stop, num) -> linspace object

    Return a virtual sequence of num numbers from start to stop (inclusive).

    If you need a half-open range, use linspace(start, stop, num+1)[:-1].
    """
    def __init__(self, start, stop, num):
        if not isinstance(num, numbers.Integral) or num <= 1:
            raise ValueError('num must be an integer > 1')
        self.start, self.stop, self.num = start, stop, num
        self.step = (stop-start)/(num-1)
    def __len__(self):
        return self.num
    def __getitem__(self, i):
        if isinstance(i, slice):
            return [self[x] for x in range(*i.indices(len(self)))]
        if i < 0:
            i = self.num + i
        if i >= self.num:
            raise IndexError('linspace object index out of range')
        if i == self.num-1:
            return self.stop
        return self.start + i*self.step
    def __repr__(self):
        return '{}({}, {}, {})'.format(type(self).__name__,
                                       self.start, self.stop, self.num)
    def __eq__(self, other):
        if not isinstance(other, linspace):
            return False
        return ((self.start, self.stop, self.num) ==
                (other.start, other.stop, other.num))
    def __ne__(self, other):
        return not self==other
    def __hash__(self):
        return hash((type(self), self.start, self.stop, self.num))
6 年前
回复了 Chris_Rands 创建的主题 » 使用python的文本文件中的motif finder

它将更健壮,使用更简单 Biopython 为此:

from Bio import SeqIO

motif = 'GGAC'
for record in SeqIO.parse('input.fa', 'fasta'):
    if motif in record.seq:
        print(record.id)