简体   繁体   English

在for循环中使用python替换功能

[英]Using python replace function in for loop

I'm trying to test out changing certain bases in a DNA sequence, but the way I have it written now is mutating all the bases of the same kind (ie: all 'A's to 'G's) instead of only changing the bases within the section of DNA I want to mutate. 我正在尝试测试DNA序列中某些碱基的改变,但是我现在编写的方式是将相同类型的所有碱基突变(即:所有'A'到'G'),而不是只改变DNA中的碱基。我要突变的DNA部分。

I know this is because of how I have my replace function input set up, but I'm not sure how to specify which base I want changed based on the for loop - whichever base is targeted in each iteration of the for loop is the only one I want to mutate within that iteration. 我知道这是因为我如何设置替换函数输入,但是我不确定如何根据for循环指定要更改的基数-for循环的每次迭代中针对的基数是唯一的我想在那次迭代中进行变异。 Here's what I have currently: 这是我目前所拥有的:

import random
word = 'GTGATCCAGT'

for base in word[5:]:
    print base
    if base == 'A':
        new_base = random.choice('CTG')
        print new_base
        new_word = word.replace(base, new_base)
        print new_word
    elif base == 'C':
        new_base = random.choice('ATG')
        print new_base
        new_word = word.replace(base, new_base)
        print new_word
    elif base == 'G':
        new_base = random.choice('CTA')
        print new_base
        new_word = word.replace(base, new_base)
        print new_word
    elif base == 'T':
        new_base = random.choice('AGC')
        print new_base
        new_word = word.replace(base, new_base)
        print new_word
    word = new_word

Thank you! 谢谢!

There is a lot of duplication in your code. 您的代码中有很多重复项。 I would suggest: 我会建议:

import random

word = list('GTGATCCAGT')
BASES = "ACGT"

for index, base in enumerate(word[:5]):
    word[index] = random.choice(BASES.replace(base, ""))

word = "".join(word)

A trial run gives me: 试运行给了我:

>>> word
'TACTACCAGT'

Note the switch to a list - strings in Python are immutable, so you can't (easily) change an individual character. 请注意切换到list -Python中的字符串是不可变的,因此您不能(轻松地)更改单个字符。 By contrast, lists are mutable, so you can switch the item at a given index without any fuss. 相反,列表是可变的,因此您可以在给定索引处切换项目,而不必大惊小怪。

When you are looping through the sequence, make sure to keep an index as well. 当您遍历序列时,请确保还保留索引。 This index will help you determine the location in the string that you want to replace. 该索引将帮助您确定要替换的字符串中的位置。 When you want to add contents to a location, simply split the string at that location into two strings. 当您要将内容添加到某个位置时,只需将该位置的字符串分成两个字符串即可。 Then concatenate the new string by adding it in the middle of the two strings and thus you end up with a new string with the correct contents. 然后,通过将新字符串添加到两个字符串的中间来连接新字符串,从而最终得到具有正确内容的新字符串。

Your loop doesn't currently have enough information to do what you want: in particular, it doesn't know which base you are currently looking at, only what its value is. 您的循环当前没有足够的信息来做您想做的事情:特别是,它不知道您当前正在看哪个基础,仅知道它的价值。 You could use the builtin enumerate to introduce that information, but a simpler way would be to change the logic so it doesn't rebuild the string each time - instead, write a generator that gives you each successive new_base , and join them all into new_word at the end. 可以使用内置的enumerate来介绍该信息,但是更简单的方法是更改​​逻辑,以便它不会每次都重新new_base字符串-而是编写一个生成器,为您提供每个连续的new_base ,并将它们全部加入到new_word在末尾。 It looks like this: 看起来像这样:

def rebase(word):
    for base in word:
        print base
        if base == 'A':
           new_base = random.choice('CTG')
           print new_base
           yield new_base
        # etc
        else:
          # If you didn't change this base, yield the original one
          yield base

 new_word = word[:5] + ''.join(rebase(word[5:]))

You might also want to use a dictionary to avoid the chain of if s - like this: 您可能还想使用字典来避免if s的链-像这样:

def rebase(word):
    possible_replacements = {'A': 'CTG', 'C': 'ATG'} # etc
    for base in word:
        print base
        try:
           yield random.choice(possible_replacements[base])
        except KeyError:            
          # If you didn't change this base, yield the original one
          yield base

new_word = word[:5] + ''.join(rebase(word[5:]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM