1

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?

5
  • print (Counter(j)["A"]/float(len(j)))
    – ZdaR
    Commented Sep 7, 2015 at 6:42
  • What happens if i do not know which element is in the sublist? For eg. If i do not know sublist has "A" and I still like percentage values for each element. Commented Sep 7, 2015 at 6:49
  • A = number of A's / total number of elements is misleading since your example shows a different output. did you mean sum of elements instead of total number of elements?
    – Mp0int
    Commented Sep 7, 2015 at 6:55
  • @Explore_SDN Please update your question with necessary details.
    – ZdaR
    Commented Sep 7, 2015 at 7:01
  • yes, it is sum of elements.c = Counter(j) s = sum(c.values()) print(c) print ( c.keys/ s) -> When I use this code for any character in sublist, I am not able to make this work. Can you tell me what is wrong? Commented Sep 7, 2015 at 7:02

4 Answers 4

1

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}]
1
  • I feel we can be more succinct [{i: x.count(i)/float(len(x)) for i in x} for x in List1] - check my answer? :-) Commented Sep 7, 2015 at 7:20
1

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}]
4
  • Now do this also for B and C, and you have the need for a counter.
    – poke
    Commented Sep 7, 2015 at 6:45
  • What happens if i do not know which element is in the sublist? For eg. If i do not know sublist has "A" and I still like percentage values for each element. Commented Sep 7, 2015 at 6:50
  • c = Counter(j) s = sum(c.values()) print(c) print ( c.keys/ s) -> When I use this code for any character in sublist, I am not able to make this work. Can you tell me what is wrong? Commented Sep 7, 2015 at 6:59
  • @Explore_SDN See my updated answer, you can achieve what you want in a simple one liner Commented Sep 7, 2015 at 7:18
1
>>> 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}
3
  • What happens if i do not know which element is in the sublist? For eg. If i do not know sublist has "A" and I still like percentage values for each element. Commented Sep 7, 2015 at 6:48
  • Indexing an element of a counter that does not exist will yield 0, so it still works. You could do it as above for all of A, B, and C, and it would output the correct percentage.
    – poke
    Commented Sep 7, 2015 at 6:50
  • c = Counter(j) s = sum(c.values()) print(c) print ( c.keys/ s) -> When I use this code for any character in sublist, I am not able to make this work. Can you tell me what is wrong? Commented Sep 7, 2015 at 7:01
0

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.