【发布时间】:2016-10-10 23:37:28
【问题描述】:
我有一个列表:
results = [['TTTT', 'CCCZ'], ['ATTA', 'CZZC']]
我创建了一个计数器,用于存储每个列表中每个元素的字符数,仅当字符为 ATGC [NOT Z] 时
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]]
**
然后我计算每个字符 (pi) 的频率,将其平方然后求和(然后我计算 het = 1 - sum)。
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.
** 代码
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]
** 我需要将 list_of_hets 中的每个元素乘以校正因子
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
最后,我需要找到 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:zip 参数 #1 必须支持迭代”
我了解在第一步更正代码可能会解决问题。我尝试手动输入“期望的输出”并运行其余代码,但没有运气。
【问题讨论】:
-
l中的zip(*l)是什么??? -
results = [['TTTT', 'CCCZ'], ['ATTA', 'CZZC']]的第一个示例中的所需输出不应该是[[4,3],[4,2]]吗? -
@juanpa.arrivillaga :我已经纠正了你的担忧。感谢您指出。
-
@mhawke:我已经纠正了你的担忧。感谢您指出。
标签: python list error-handling append