私はリストのリストを持っている: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は、反復処理をサポートしなければならないジップ」を取得
を私が最初のステップで、コードを修正すると、それを解決することを理解。私は手動で "希望の出力"を入力し、コードの残りの部分を実行しようとしましたが、運はありません。
「zip(* l)」には「l」とは何ですか? –
関連:http://stackoverflow.com/questions/39966187/how-to-zip-a-list-of-lists-in-python –
最初の例で 'results = [[' TTTT '、' CCCZ ']、[' ATTA '、' CZZC ']] '[[4,3]、[4,2]]'とすればよいのですか? – mhawke