简体   繁体   English

Python:计数器输出上的计数元素

[英]Python: Count elements on Counter output

I have a nested list as: 我有一个嵌套列表:

 List1 = [[A,B,A,A],[C,C,B,B],[A,C,B,B]]..... so on

I used counter function to count the number of elements in the nested lists: 我使用counter函数来计算嵌套列表中的元素数量:

for i,j in enumerate(List1):
    print(Counter(j))

I got following output as: 我得到以下输出:

Counter({'A': 3, 'B': 1})
Counter({'C': 2, 'B': 2})
Counter({'B': 2, 'A': 1, 'C': 1})
....

I want to calculate percentage of A in Counter output: 我想计算Counter输出中A的百分比:

A = number of A's / total number of elements

For example: 例如:

Counter({'A': 3, 'B': 1})

Would yield: 会产量:

A = 3/4 = 0.75

I am not able to calculate A, Can anyone kindly help me with this? 我无法计算A,有人可以帮我这个吗?

>>> for sublist in List1:
        c = Counter(sublist)
        print(c['A'] / sum(c.values()))

0.75
0.0
0.25

All values at once: 所有值一次:

>>> for sublist in List1:
        c = Counter(sublist)
        s = sum(c.values())
        print(c['A'] / s, c['B'] / s, c['C'] / s)

0.75 0.25 0.0
0.0 0.5 0.5
0.25 0.5 0.25

If you want to get a list of all items in a sublist with their respective percentages, you need to iterate the counter: 如果要获取子列表中具有各自百分比的所有项目的列表,则需要迭代计数器:

>>> for sublist in List1:
        c = Counter(sublist)
        s = sum(c.values())
        for elem, count in c.items():
            print(elem, count / s)
        print()

A 0.75
B 0.25

B 0.5
C 0.5

A 0.25
B 0.5
C 0.25

Or use a dictionary comprehension: 或者使用词典理解:

>>> for sublist in List1:
        c = Counter(sublist)
        s = sum(c.values())
        print({ elem: count / s for elem, count in c.items() })

{'A': 0.75, 'B': 0.25}
{'B': 0.5, 'C': 0.5}
{'A': 0.25, 'B': 0.5, 'C': 0.25}

This: 这个:

In [1]: l = [['A','B','A','A'],['C','C','B','B'],['A','C','B','B']]

In [2]: [{i: x.count(i)/float(len(x)) for i in x} for x in l]
Out[2]:
[{'A': 0.75, 'B': 0.25},
 {'B': 0.5, 'C': 0.5},
 {'A': 0.25, 'B': 0.5, 'C': 0.25}]

The following would give you a list of dictionaries holding both the counts and the percentages for each entry: 以下内容将为您提供一个字典列表,其中包含每个条目的计数和百分比:

List1 = [['A','B','A','A'],['C','C','B','B'],['A','C','B','B']]
counts = [Counter(x) for x in List1]
percentages = [{k : (v, v / float(len(l1))) for k,v in cc.items()} for l1, cc in zip(List1, counts)]

print percentages

Giving the following output: 给出以下输出:

[{'A': (3, 0.75), 'B': (1, 0.25)}, {'C': (2, 0.5), 'B': (2, 0.5)}, {'A': (1, 0.25), 'C': (1, 0.25), 'B': (2, 0.5)}]

For just the percentages: 只是百分比:

List1 = [['A','B','A','A'],['C','C','B','B'],['A','C','B','B']]
counts = [Counter(x) for x in List1]
percentages = [{k : v / float(len(l1)) for k,v in cc.items()} for l1, cc in zip(List1, counts)]

print percentages

Giving: 赠送:

[{'A': 0.75, 'B': 0.25}, {'C': 0.5, 'B': 0.5}, {'A': 0.25, 'C': 0.25, 'B': 0.5}]

You can use list generator and join method to connect your lists of lists of chars into one-liner list of strings. 您可以使用列表生成器和连接方法将字符列表列表连接到一行字符串列表中。

>>> List1 = [['A', 'B', 'A', 'A'],['C', 'C', 'B', 'B'],['A', 'C', 'B', 'B']]
>>> [''.join(x) for x in List1]
['ABAA', 'CCBB', 'ACBB']

Then, join again your list to the one string. 然后,再次将列表连接到一个字符串。

>>> ''.join(['ABAA', 'CCBB', 'ACBB'])
'ABAACCBBACBB'

And count 'A' symbol, or any other. 并计算'A'符号或任何其他符号。

>>> 'ABAACCBBACBB'.count('A')
4

This could be one-liner solution: 这可能是单线解决方案:

>>> ''.join(''.join(x) for x in List1).count('A')
4

String of symbols is iterable type. 符号字符串是可迭代类型。 The same as the list. 与列表相同。 List of strings is more useful than the list of lists of chars. 字符串列表比字符列表列表更有用。

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

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