【问题标题】:How to use counters and zip functions with a list of lists in Python?如何在 Python 中使用带有列表列表的计数器和 zip 函数?
【发布时间】: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


【解决方案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]

【讨论】:

  • 我需要输入一个校正因子,即计数/(计数-1)。 Correctionfactor = banansplit(我已经做出了改变)。 tt = 样本(我已经做出改变)
  • 我将您对 Part1 的命令修改为 d = [[(sum(c in BASES for c in s)-1) for s in pair] for pair in results] correctionfactor = [float(b) / float(m) for b,m in zip(counts, d)] 。但是,correctionfactor 似乎给出了错误
  • @Biotechgeek:重新修正系数,如果只有一个有效碱基,你会怎么做?例如ZZZA 的校正因子将导致除以零错误。
  • 我知道,这是对这种方法的限制。我们以单独的方式处理它,因此我没有将其纳入其中,因为它可能会使观众感到困惑。
【解决方案2】:

第一个地图遍历结果。 第二张地图替换“Z”并计算元素。

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

【讨论】:

  • 谢谢。关于如何让 Correctionfactor 或 list_of_hets 工作的任何想法?
  • Correctionfactor 只是 nn 除以 d。 (即nn除以(nn-1))。但是,我不能做一个简单的划分。我得到这个错误:TypeError: float() argument must be a string or a number
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-10
  • 1970-01-01
  • 2010-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-12
相关资源
最近更新 更多