Python | Make the Percentage of a List

Python | Make the percentage of a list

You already have an integer, r. You can just use r-1 as the index to sommes

from random import randint
n=int(input("n="))
sommes = [0]*12
for i in range(1,n+1):
r=randint(1,6)+randint(1,6)
sommes[r-1] += 1

For the percentages part you can just divide everything by n and multiply by 100:

y = [int(x/n*100) for x in sommes]

(although if you use integers as in your example, it won't add up to 100).
or if you want a list of strings with percentages you can use this format. This does have the advantage that you can control how many decimal places the strings have. .0f is 0, but you could change the 0 for the number of decimal places you want.

y = [f"{x/n*100:.0f}%" for x in sommes]

I've also optimised the inputs as it's not efficient to use from random import * if you're only using randint

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

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:

sequence=list(zip(*['123', '134', '234', '214', '223']))
percentages=[]
for x in sequence:
t=list(x)
temp=[t.count(str(i))/len(x) for i in range(1,5)] #work out the percentage of each number
percentages.append(temp) #add percentages to list

Or, as one list comprehension:

percentages=[[list(x).count(str(i))/len(x) for i in range(1,5)]for x in sequence]

Output:

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

Percent list slicing

originalList.sort()
newList = originalList[int(len(originalList) * .05) : int(len(originalList) * .95)]

Python calculate percentage of list values one by others

is this what you want?

x =["3150000", "3156000", "3150000","3250000"]
x = list(map(int, x))

for x1 in x:
for x2 in x:
if x1 == x2:
continue

diff_per = abs(x1 - x2) / x1 * 100
print("abs({x1} - {x2}) / {x1} * 100 == {:.2f}%".format(diff_per, x1=x1, x2=x2))

output:

abs(3150000 - 3156000) / 3150000 * 100 == 0.19%
abs(3150000 - 3250000) / 3150000 * 100 == 3.17%
abs(3156000 - 3150000) / 3156000 * 100 == 0.19%
abs(3156000 - 3150000) / 3156000 * 100 == 0.19%
abs(3156000 - 3250000) / 3156000 * 100 == 2.98%
abs(3150000 - 3156000) / 3150000 * 100 == 0.19%
abs(3150000 - 3250000) / 3150000 * 100 == 3.17%
abs(3250000 - 3150000) / 3250000 * 100 == 3.08%
abs(3250000 - 3156000) / 3250000 * 100 == 2.89%
abs(3250000 - 3150000) / 3250000 * 100 == 3.08%

Percentage distribution of values in a python list

You can try something like this:

responseTimes = [1, 2, 3, 3, 4, 5, 6, 7, 9, 1]
for time in range(3,10):
percentage = len([x for x in responseTimes if x <= time])/(len(responseTimes))
print(f'{percentage*100}%')

"So basically lets say at 50%, we need to find point in list below which 50% of the list elements are present and so on"

responseTimes = [1, 2, 3, 3, 4, 5, 6, 7, 9, 1]
percentage = 0
time = 0
while(percentage <= 0.5):
percentage = len([x for x in responseTimes if x <= time])/(len(responseTimes))
time+=1

print(f'Every time under {time}(ms) occurrs lower than 50% of the time')


Related Topics



Leave a reply



Submit