简体   繁体   中英

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:

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:

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?

>>> 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.

>>> '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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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