2016-10-10 9 views
0

私はリストのリストを持っている:counterとzip関数をPythonのリストのリストで使うには?

results = [['TTTT', 'CCCZ'], ['ATTA', 'CZZC']] 

私は文字がATGC [Z NOT]

The desired output is [[4,3],[4,2]] 

ある場合にのみ、各リストの各要素の文字数番号を格納するカウンタを作成します**

コード:**

counters = [Counter(sub_list) for sub_list in results] 
    nn =[] 
    d = [] 
    for counter in counters: 
      atgc_count = sum((val for key, val in counter.items() if key in "ATGC"))  
      nn.append(atgc_count) 
d = [i - 1 for i in nn] 
correctionfactor = [float(b)/float(m) for b,m in zip(nn, d)] 
print nn 
print correctionfactor 

"Failed" Output: 
[0, 0] 
<closed file 'c:/test/zzz.txt', mode 'r' at 0x02B46078> 

Desired Output 
nn = [[4,3],[4,2]] 
correctionfactor = [[1.33, 1.5],[1.33,2]] 

そして、私は各文字(π)の周波数を計算し、それを二乗して合計します(そして、het = 1 - sumを計算します)。 ** コード**

list_of_hets = [] 
for idx, element in enumerate(sample): 
    count_dict = {} 
    square_dict = {} 
    for base in list(element): 
     if base in count_dict: 
      count_dict[base] += 1 
     else: 
      count_dict[base] = 1 
    for allele in count_dict: 
     square_freq = (count_dict[allele]/float(nn[idx]))**2 
     square_dict[allele] = square_freq   
    pf = 0.0 
    for i in square_dict: 
     pf += square_dict[i] # pf --> pi^2 + pj^2...pn^2 
    het = 1-pf      
    list_of_hets.append(het) 
print list_of_hets 

"Failed" OUTPUT: 
[-0.0, -0.0] 

The desired output [[1,2],[1,2]] #NOTE: This is NOT the real values of expected output. I just need the real values to be in this format. 

私は、補正係数によって

h = [float(n) * float(p) for n,p in zip(correction factor,list_of_hets) 
With the values given above: 
h = [[1.33, 1.5],[1.33,2]] #correctionfactor multiplied by list_of_hets 

をlist_of_hets内のすべての要素を乗算する必要が最後に、私はすべての平均値を見つける必要があります要素をhに格納し、新しいリストに格納します。

The desired output should read as [1.33, 1.75]. 

私はこの例(Sum of list of lists; returns sum list)を試してみました。

hs = [mean(i) for i in zip(*h)] 

しかし、私は次のようなエラー「はTypeErrorを:引数#1は、反復処理をサポートしなければならないジップ」を取得

を私が最初のステップで、コードを修正すると、それを解決することを理解。私は手動で "希望の出力"を入力し、コードの残りの部分を実行しようとしましたが、運はありません。

+4

「zip(* l)」には「l」とは何ですか? –

+0

関連:http://stackoverflow.com/questions/39966187/how-to-zip-a-list-of-lists-in-python –

+0

最初の例で 'results = [[' TTTT '、' CCCZ ']、[' ATTA '、' CZZC ']] '[[4,3]、[4,2]]'とすればよいのですか? – mhawke

答えて

1

最初の部分は次のように行うことができます:あなたがカウントを有する

BASES = {'A', 'C', 'G', 'T'} 

results = [['TTTT', 'CCCZ'], ['ATTA', 'CZZC']] 
counts = [[sum(c in BASES for c in s) for s in pair] for pair in results] 
>>> counts 
[[4, 3], [4, 2]] 

たら、補正係数も、リスト内包して計算することができます。

correction_factors = [[i/float(i-1) for i in pair] for pair in counts] 
>>> correction_factors 
[[1.3333333333333333, 1.5], [1.3333333333333333, 2.0]] 

しかし、あなたはする必要がありますかカウントが1である場合に注意してください。これは、ゼロによる除算につながります。私はあなたがそれをどう扱うべきか分かりません... 1の値は適切でしょうか?

correction_factors = [[i/float(i-1) if i-1 else 1 for i in pair] for pair in counts] 
+0

私はカウント/(counts-1)である補正係数を入れる必要があります。 Correctionfactor = banansplit(私は変更を加えました)。 – Biotechgeek

+0

Part1のコマンドを 'd = [[(sのBASESのcでsを足して)-1] -1のように変更しました]結果のペアのために' '補正係数= [float(b)/ float(m)for b、m in zip(counts、d)] '。しかし、補正係数は誤差を与えているようです – Biotechgeek

+0

@Biotechgeek:補正係数は、有効な基底が1つだけの場合はどうしますか?例えば「ZZZA」に対する補正係数は、ゼロ誤差による除算につながる。 – mhawke

0

最初のマップトラバースの結果。 2番目のマップは 'Z'とcount要素を置き換えます。

map(lambda x:map(lambda y:len(y.replace('Z','')),x),l) 
+0

ありがとうございます。どのようにcorrectfactorまたはlist_of_hetsを取得するための任意のアイデアは動作しますか? – Biotechgeek

+0

補正係数のルールは? – zxy

+0

補正係数は、単にnnをdで割ったものです。 (すなわち、nnを(n-n-1)で除算した)。しかし、私は単純な分裂をすることはできません。私はこのエラーが発生します:TypeError:float()引数は文字列または数値でなければなりません – Biotechgeek

関連する問題