我有一個列表的列表:如何通過Python中的for循環傳遞列表列表?
sample = [['TTTT', 'CCCZ'], ['ATTA', 'CZZC']]
count = [[4,3],[4,2]]
correctionfactor = [[1.33, 1.5],[1.33,2]]
我計算每個字符(PI)的頻率,將其平方和然後總和(和然後我計算HET = 1 - 總和)。
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.
問題:我不怎麼通過列表(樣本,計數)在這個循環中提取所需的值。我以前只通過一個列表(例如['TACT','TTTT'..]
)使用此代碼。
- 我懷疑我需要添加環路越大,指數在每個元素的樣品(即指數超過
sample[0] = ['TTTT', 'CCCZ']
和sample[1] = ['ATTA', 'CZZC']
。我不知道如何將其寫入代碼。
** 代碼
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: #Calculate frequency of every character
square_freq = (count_dict[allele]/count[idx])**2 #Square the frequencies
square_dict[allele] = square_freq
pf = 0.0
for i in square_dict:
pf += square_dict[i] # pf --> pi^2 + pj^2...pn^2 #Sum the frequencies
het = 1-pf
list_of_hets.append(het)
print list_of_hets
"Failed" OUTPUT:
line 70, in <module>
square_freq = (count_dict[allele]/count[idx])**2
TypeError: unsupported operand type(s) for /: 'int' and 'list'er
錯誤消息告訴您確切** **什麼是錯的:'square_freq =(count_dict [等位基因] /計數[IDX])** 2'正在引發'TypeError:不支持的操作數類型(s)爲/:'int'和'list'。你不能用'list'來劃分'int'。順便說一下,這與您編寫的代碼不匹配,當您嘗試將計數[idx]傳遞給「float」時,可能會引發另一個「TypeError」。 –
我想使用一個zip命令,如'square_freq = [[n/d for n,d in zip(subq,subr)] for subq,subr in zip(count_dict [allele],counts)]''。但我仍然有錯誤。還有其他建議嗎? – biogeek
@ PM2Ring我已糾正它。感謝您指出 – biogeek