Làm thế nào để bạn tìm thấy tỷ lệ phần trăm của một phần tử trong danh sách python?

Steven D'Aprano steve tại pearwood. thông tin
Thứ hai Tháng chín 1 03. 46. 04 CEST 2014
Hello,

You seem to have deliberately set the Reply-To header for replies to go 
back to you, instead of allowing them to go to the mailing list for 
others to take part in the conversation. I don't know if that was you 
personally, or just another sign of the major suckage that is Yahoo 
mail, but either way I have honoured that request this time, but 
normally replies to questions on the tutor mailing list should go to 
the mailing list so that the others may learn.

More comments below, interleaved with your questions.


On Sun, Aug 31, 2014 at 10:28:32AM -0700, LN A-go-go wrote:
> 
> 
> What would be a better way to calculate percentages of items in a list?
> please....
> 
> CountList = [9221382, 10374466, 5192905, 1710238, 3359]
> CL = [float(i) for i in CountList]

I really don't know why you convert the numbers into floats. There 
doesn't seem to be any reason for that -- you can sum integer numbers 
just as easily. If you insist on floats, why not just make them floats 
in the first place?

CountList = [9221382.0, 10374466.0, 5192905.0, 1710238.0, 3359.0]

but that's silly. If they are meant to be *counts*, then you cannot have 
a fraction of a count. Just keep them as ints.

> CL
> sum = CL[0] + CL[1] + CL[2] + CL[3] + CL[4]

Here you accidentally "shadow" the built-in sum() function. You 
shouldn't do this unless you know what you are doing. Instead:

total = sum(CountList)

gives you the total you are after.


> import math

That's not used or needed.


> perList = []
> n = 0
> def percentage(CL,sum):
>  for i in CL:
>      PER = "return 100 * float (CL[0])/float (sum)"
>      perList.append(PER)
>      n = n + 1

You appear to be wanting to calculate cumulative percentages. A simple 
example:

CountList = [5, 2, 8, 5]
total = 20
cumulative percentages = [25.0, 35.0, 75.0, 100.0]

Is that correct? If so, this will do it:

counts = [9221382, 10374466, 5192905, 1710238, 3359]
cumulative_totals = []
last = 0
for value in counts:
    last += value
    cumulative_totals.append(last)

total = sum(counts)
# check that the last value matches the total:
assert total == cumulative_totals[-1]  
cumulative_percentages = []
for value in cumulative_totals:
    perc = value*100.0/total
    cumulative_percentages.append(perc)

print(cumulative_percentages)

I think that should do the trick.


-- 
Steven


Thông tin thêm về danh sách gửi thư Tutor
Không có toán tử phần trăm nào trong Python để tính phần trăm, nhưng nó không thích hợp để bạn tự thực hiện

Tỷ lệ pPython

Để tính tỷ lệ phần trăm trong Python, hãy sử dụng toán tử chia (/) để lấy thương từ hai số rồi nhân thương này với 100 bằng toán tử nhân (*) để lấy tỷ lệ phần trăm

quotient = 3 / 5

percent = quotient * 100

print(percent)

đầu ra

60.0

Điều đó có nghĩa là nó là 60%

Bạn có thể tạo một hàm tùy chỉnh trong Python để tính tỷ lệ phần trăm

def percentage(part, whole):
  percentage = 100 * float(part)/float(whole)
  return str(percentage) + "%"

print(percentage(3, 5))

đầu ra

60.0%

Bạn có thể muốn thêm một câu lệnh if, toàn bộ là 0 trả về 0 vì nếu không, điều này sẽ tạo ra một ngoại lệ

Nếu bạn muốn tỷ lệ phần trăm của nhau, bạn cần sử dụng đoạn mã sau

def percent(x, y):
    if not x and not y:
        print("x = 0%\ny = 0%")
    elif x < 0 or y < 0:
        print("The inputs can't be negative!")
    else:
        final = 100 / (x + y)
        x *= final
        y *= final
        print('x = {}%\ny = {}%'.format(x, y))

percent(3, 6)

đầu ra

x = 33.33333333333333%
y = 66.66666666666666%

Dấu % Python(Toán tử Modulo)

Dấu phần trăm trong Python được gọi là toán tử modulo “%”, trả về phần còn lại sau khi chia toán hạng bên trái cho toán hạng bên phải

data = 3
info = 2
remainder = data % info
print(remainder)

đầu ra

________số 8

Đầu ra sẽ xuất hiện dưới dạng “1“. Ở đây, toán tử modulo “%” trả về phần còn lại sau khi chia hai số

Toán tử %s cho phép bạn thêm giá trị vào chuỗi Python. %s biểu thị rằng bạn muốn thêm một giá trị chuỗi vào chuỗi; . Thế là xong.

Bài viết liên quan

Chế độ Python

divmod Python

fmod Python

chế độ Python

Facebook

Twitter

Pinterest

WhatsApp

Bài viết trước Cách định dạng giá trị nổi trong Python

Bài viết tiếp theo 6 cách dễ dàng để lấy phần tử cuối cùng của danh sách trong Python

Làm thế nào để bạn tìm thấy tỷ lệ phần trăm của một phần tử trong danh sách python?

kranal

https. //appdividend. com/

Krunal Lathiya là Kỹ sư phần mềm với hơn tám năm kinh nghiệm. Anh ấy đã phát triển một nền tảng vững chắc về các nguyên tắc khoa học máy tính và niềm đam mê giải quyết vấn đề. Ngoài ra, Krunal có kiến ​​thức tuyệt vời về Khoa học dữ liệu và Học máy, đồng thời là chuyên gia về Ngôn ngữ Python. Krunal có kinh nghiệm với nhiều ngôn ngữ lập trình và công nghệ khác nhau, bao gồm PHP, R, Golang và JavaScript. Anh ấy cảm thấy thoải mái khi làm việc trong lĩnh vực phát triển front-end và back-end