## How to calculate the percentage of each element in a list?

Question

I have this list with 5 sequence of numbers:

```
['123', '134', '234', '214', '223']
```

and I want to obtain the percentage of each number `1, 2, 3, 4`

in the `ith`

position of each sequence of numbers. For example, the numbers at `0th`

position of this `5`

sequences of numbers are `1 1 2 2 2`

, then I need to calculate the percentage of
`1, 2, 3, 4`

in this sequence of numbers and return the percentage as `0th`

element of a new list.

```
['123', '134', '234', '214', '223']
0th position: 1 1 2 2 2 the percentage of 1,2,3,4 are respectively: [0.4, 0.6, 0.0, 0.0]
1th position: 2 3 3 1 2 the percentage of 1,2,3,4 are respectively: [0.2, 0.4, 0.4, 0.0]
2th position: 3 4 4 4 3 the percentage of 1,2,3,4 are respectively: [0.0, 0.0, 0.4, 0.6]]
```

Then desired result is to return:

```
[[0.4, 0.6, 0.0, 0.0], [0.2, 0.4, 0.4, 0.0], [0.0, 0.0, 0.4, 0.6]]
```

My attempt so far:

```
list(zip(*['123', '134', '234', '214', '223']))
```

Result:

```
[('1', '1', '2', '2', '2'), ('2', '3', '3', '1', '2'), ('3', '4', '4', '4', '3')]
```

But I got stuck here, then I don't know how to calculate the percentage of the element of each numbers of `1, 2, 3, 4`

, then obtain the desired result. Any suggestion is appreciated!

Show source

## Answers ( 3 )

You can use

`count(i)`

to determine to number of occurrences of the numbers 1-4 and divide it by 5 to obtain the percentage:Or, as one list comprehension:

Output:

starting from your approach, you could do the rest with a

`Counter`

which prints

You could start by creating the zipped list as you did:

then map an

`itertools.Counter`

to it as to get the counts of each item in the results from`zip`

:and then go through it, creating a list out of their counts divided by their sizes:

If you are a one-liner kind of person, mush the first two in the comprehension to get this in one line:

additionally, as noted in a comment, if you don't know the elements ahead of time,

`sorted(set(''.join(l)))`

could replace`'1234'`

.