python - 将字符串中的随机值替换为随机值

标签 python string python-3.x list random

病毒应该是一串核苷酸,函数应该返回一个由相同数量的核苷酸组成的字符串,但其中一个被改变了。

def mutate(virus):
    mutations = ['A', 'T', 'C', 'G']
    virus.split
    random.randrange(1, stop=len(virus), step=1) = random.choice(mutations)

例如,如果病毒是 ATCG,它应该返回类似 ATCC 或 GTCG 的内容,我该如何处理这个问题,我尝试将病毒放入一个列表中,并用我可能的突变列表中的随机变量替换其中的随机变量.

所以它可能应该从字符串病毒中创建一个列表,进行突变,将列表放回到字符串中并返回字符串。

最佳答案

你可以做某事。像下面这样:

def mutate(virus):
    # choose random index to change
    index = random.randint(0, len(virus) - 1)

    # make sure you are not using the previous char by removing it from 
    # the mutations to choose from
    mutations = [c for c in 'ATCG' if c != virus[index]]

    # swap out the char at index with a random mutation
    return virus[:index] + random.choice(mutations) + virus[index+1:]

关于python - 将字符串中的随机值替换为随机值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47702285/

相关文章:

当我用变量替换硬编码值时,Python SQL 查询返回 "ORA-00936: missing expression"

python - 如何让 urllib2 以数组形式返回数据

python - 哪些 PyTorch 模块受 model.eval() 和 model.train() 影响?

python-3.x - python-gitlab api SSL 握手错误 : Error([ ('SSL routines' , 'ssl3_get_server_certificate' , 'certificate verify failed' )],

java - 简单手机号码的正则表达式(Scala 风格)

java - 使用相同的字符串部分进行正则表达式检查

python - 当值具有实体时,Selenium WebDriver get_attribute 返回 href 属性的截断值

带导入的 Python 函数作用域

python - 减少嵌套字典

c# - MarshalAsAttribute 字符串数组