2016-11-03 2 views
0
def mutations (list_a,string1,name,list_b): 
    """ (list of str, str, list of str, list of str) -> NoneType 
    """ 


    dna=list_a 
    for i in range(len(list_b)): 
     strand=dna[:dna.index(list_b[i])] 
     string1=string1[string1.index(list_b[i]):] 
     dna[strand+string1] 

>>>dna=['TGCAGAATTCGGTT','ACGTCCCGGGTTGC'] 
>>>mutations(dna,'CCCGGGGAATTCTCGC',['EcoRI','SmaI'],['GAATTC','CCCGGG']) 
>>>mutated 
>>>['TGCAGAATTCTCGC','ACGTCCCGGGGAATTCTCGC'] 

パイソン。そこで、基本的list_aを変更しようと、それはしかし['TGCAGAATTCTCGC','ACGTCCCGGGGAATTCTCGC']に変更することイム、私は変異DNAは、最初のパラメータを変更することとしています

strand=dna[:dna.index(string1[i])]. 

ValueError: 'GAATTC' is not in list 

を言って、エラーを取得する。また、シーケンスが存在しない場合、それは機能を変更しない方法はありますか?

+0

はSTRのリストで、クリーンでSTRで、配列は、デバッグのヘルプ(「なぜISNを求めている認識配列(DNA鎖) – CAVS

+0

質問ですこのコードは動作していますか?)には、目的の動作、特定の問題またはエラー、および質問自体の中でそれを再現するのに必要な最短コードが含まれていなければなりません。明確な問題文がない質問は、他の読者にとって有用ではありません。 「MCVEの作成方法」を参照してください。 –

+2

@jojo申し訳ありませんが、コード – CAVS

答えて

1

を(最初のDNA要素が変更され、第二はしませんでした)。その場合、list_aから残りの文字列(list_b要素を含む)を、list_bの要素を含む制御文字列の一部に置き換えることで、要素を修正したいと思いますか?

理想的には、これをあなたの質問に入れてください!

これを行う方法は、以下のようになります:突然変異した

def mut(list_a, control, list_b): 
    check_others = Falsee 
    for i in range(len(list_a)): # run through list_a (use xrange in python 2.x) 
     if i == len(list_b): # if we are at the end of list_b we run will 
      # check all other elements if list_b (see below) 
      check_others = True 
     if not check_others: # this is the normal 1 to 1 match. 
      if list_b[i] in list_a[i]: # if the element from list_b is in it 
       # correct the element 
       list_a[i] = list_a[i][:list_a[i].index(list_b[i])] +\ 
        control[control.index(list_b[i]):] 
     else: # this happens if we are at the end of list_b 
      for j in xrange(len(list_b)): # run through list_b for the start 
       if list_b[j] in list_a[i]: 
        list_a[i] = list_a[i][:list_a[i].index(list_b[j])] +\ 
         control[control.index(list_b[j]):] 
        break # only the first match with an element in list_b is used! 
+0

何かを返すとは思わない、 – CAVS

+0

xrangeが定義されていないというエラーが出る – CAVS

+0

Python3 'range'はPython2' xrange' @CAVS –

0

コメントに記載されているように、dnaは文字列ではなくリストであるため、部分文字列を見つけることはあなたの望むようにはうまくいかないでしょう。

dna=list_aはありません。dna[strand+string1]はリストを変更しないため、そこで達成しようとしていることがわかりません。

私は次のコードがあなたが期待している出力を得ていないことを知っていますが、うまくいけばより正確なパスに設定されます。

def mutations (mutated,clean,recognition): 
    """ (list of str, str, list of str) -> NoneType 
    """ 

    # Loop over 'mutated' list. We need the index to update the list 
    for i,strand in enumerate(mutated): 

     # Loop over 'recognition' list 
     for rec in recognition: 

      # Find the indices in the two strings 
      strand_idx = strand.find(rec) 
      clean_idx = clean.find(rec) 

      # Check that 'rec' existed in both strings 
      if strand_idx > 0 and clean_idx > 0: 
       # both are found, so get the substrings 
       strand_str = strand[:strand_idx] 
       clean_str = clean[clean_idx:] 

       # debug these values 
       print(rec, (strand_idx, strand_str,), (clean_idx, clean_str,)) 

       # updated 'mutated' like this 
       mutated[i] = strand_str+clean_str 

そして、出力を(それが使用されていなかったので、私はnameを削除しました)。私は、正しくあなたを理解し、それがlist_bからそれに対応する要素が含まれている場合はlist_aの各要素をチェックしたい場合はまあ

dna=['TGCAGAATTCGGTT','ACGTCCCGGGTTGC'] 
mutations(dna,'CCCGGGGAATTCTCGC',['GAATTC','CCCGGG']) 

print(dna) # ['TGCAGAATTCTCGC', 'ACGTCCCGGGTTGC'] 
+0

cricket_007列挙型を使用するのではなく、他にもできることはありますか – CAVS

+0

リストを更新するには、現在の値のインデックスが必要です。私はあなたが何を考えているのかわからない –

関連する問題