简体   繁体   English

在Python中更改计数器的输出

[英]Changing the output of counter in python

I'm using the following code to unzip a dictionary and count the values at each site: 我使用以下代码解压缩字典并计算每个站点的值:

result = [Counter(site) for site in zip(*myDict.values())]

The output looks something like: Counter({'A': 74}), Counter({'G': 72, 'C': 2}) There are five possible values: A, T, G, C , and N 输出类似于: Counter({'A': 74}), Counter({'G': 72, 'C': 2})有五个可能的值: A, T, G, CN

I only want the counter to spit out a value if one of the five values is less than 74. So for the above example, only the second would be outputted. 我只希望计数器在五个值之一小于74时吐出一个值。因此对于上面的示例,仅输出第二个值。 How do you use an if statement within the counter? 您如何在计数器中使用if语句? Furthermore, how can I label each site, so that above it could just say: 此外,我如何标记每个站点,以便在其上方仅可以说:

Site 2: 'G': 72, 'C': 2

myDict looks like this: myDict看起来像这样:

{'abc123': ATGGAGGACGACT, 'def332': ATGCATTGACGC}

Except there are 74 entries. 除了有74个条目。 Each value is the same length. 每个值的长度相同。 Basically, I don't know how to use a counter that can give me an output for when each site of each value doesn't match up. 基本上,我不知道如何使用一个计数器来为每个值的每个站点都不匹配提供输出。 So for the sequences above, the 4th site does not match. 因此,对于上述序列,第四个位点不匹配。 I want the counter to output the following: 我希望计数器输出以下内容:

site 4: 'G': 1, 'C': 1

You can use enumerate to index the sites and the most_common method on Counter can be used to check if the count is < 74. Here's an example with just two strings: 您可以使用枚举为站点建立索引, Counter上的most_common方法可用于检查计数是否小于74。这是一个只有两个字符串的示例:

from collections import Counter
myDict = {'a':'ATGTTCN','b':'ATTTCCG'}
result = [(i,Counter(site)) for i,site in enumerate(zip(*myDict.values()))]
result = [x for x in result if x[1].most_common()[0][1] < 2]
for site,count in result:
    print 'Site {}: {}'.format(site,str(count)[9:-2])

Output: 输出:

Site 2: 'T': 1, 'G': 1
Site 4: 'C': 1, 'T': 1
Site 6: 'G': 1, 'N': 1

using Dict Comprehension and only storing values if max(Counter(x).values())<74 , use enumerate() to get the Site number. 使用Dict Comprehension并仅在max(Counter(x).values())<74存储值,请使用enumerate()获取Site编号。

>>> mydict={'abc123': 'ATGGAGGACGACT', 'def332': 'ATGCATTGACGC'}
>>> result={'Site {}'.format(i+1):Counter(x) for i,x in enumerate(zip(*mydict.values())) if max(Counter(x).values())<2}
>>> result
{'Site 7': Counter({'T': 1, 'G': 1}), 'Site 6': Counter({'T': 1, 'G': 1}), 'Site 4': Counter({'C': 1, 'G': 1}), 'Site 9': Counter({'A': 1, 'C': 1}), 'Site 8': Counter({'A': 1, 'G': 1}), 'Site 11': Counter({'A': 1, 'G': 1}), 'Site 10': Counter({'C': 1, 'G': 1})}

or convert Counter to dict : 或将Counter转换为dict

>>> {'Site {}'.format(i+1):dict(Counter(x)) for i,x in enumerate(zip(*mydict.values())) if max(Counter(x).values())<2}

{'Site 7': {'T': 1, 'G': 1}, 'Site 6': {'T': 1, 'G': 1}, 'Site 4': {'C': 1, 'G': 1}, 'Site 9': {'A': 1, 'C': 1}, 'Site 8': {'A': 1, 'G': 1}, 'Site 11': {'A': 1, 'G': 1}, 'Site 10': {'C': 1, 'G': 1}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM